<!doctype html>



  


<html class="theme-next muse use-motion">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  
    
      
    

    
  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Microsoft YaHei:300,300italic,400,400italic,700,700italic|consolas:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.0.2" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="js," />








  <link rel="shortcut icon" type="image/x-icon" href="http://ofy7k3v4s.bkt.clouddn.com/mylogo.png?v=5.0.2" />






<meta name="description" content="js &amp;#x9AD8;&amp;#x7A0B;&amp;#x4E2D;&amp;#x8FD9;&amp;#x6837;&amp;#x5B9A;&amp;#x4E49;&amp;#x51FD;&amp;#x6570;&amp;#xFF1A;&amp;#x51FD;&amp;#x6570;&amp;#x662F;&amp;#x8FD9;&amp;#x6837;&amp;#x4E00;&amp;#x6BB5; JavaScript &amp;#x4EE3;&amp;#x7801;&amp;#xFF0C;&amp;#x53EA;&amp;#x5B9A;&amp;#x4E49;&amp;">
<meta property="og:type" content="article">
<meta property="og:title" content="js 的函数">
<meta property="og:url" content="https://hongqinma.github.io/2014/05/24/10-js-的函数/index.html">
<meta property="og:site_name" content="HongqinMa' Blog">
<meta property="og:description" content="js &amp;#x9AD8;&amp;#x7A0B;&amp;#x4E2D;&amp;#x8FD9;&amp;#x6837;&amp;#x5B9A;&amp;#x4E49;&amp;#x51FD;&amp;#x6570;&amp;#xFF1A;&amp;#x51FD;&amp;#x6570;&amp;#x662F;&amp;#x8FD9;&amp;#x6837;&amp;#x4E00;&amp;#x6BB5; JavaScript &amp;#x4EE3;&amp;#x7801;&amp;#xFF0C;&amp;#x53EA;&amp;#x5B9A;&amp;#x4E49;&amp;">
<meta property="og:updated_time" content="2016-12-09T02:00:36.124Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="js 的函数">
<meta name="twitter:description" content="js &amp;#x9AD8;&amp;#x7A0B;&amp;#x4E2D;&amp;#x8FD9;&amp;#x6837;&amp;#x5B9A;&amp;#x4E49;&amp;#x51FD;&amp;#x6570;&amp;#xFF1A;&amp;#x51FD;&amp;#x6570;&amp;#x662F;&amp;#x8FD9;&amp;#x6837;&amp;#x4E00;&amp;#x6BB5; JavaScript &amp;#x4EE3;&amp;#x7801;&amp;#xFF0C;&amp;#x53EA;&amp;#x5B9A;&amp;#x4E49;&amp;">



<script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '6347850919329138000',
      author: 'Hiraku'
    }
  };
</script>




  <link rel="canonical" href="https://hongqinma.github.io/2014/05/24/10-js-的函数/"/>


  <title> js 的函数 | HongqinMa' Blog </title>
</head>

<body itemscope itemtype="//schema.org/WebPage" lang="en">

  



  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?10010323";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>








  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="//schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta custom-logo">
  
    <div class="site-meta-headline">
      <a>
        <img class="custom-logo-image" src="http://ofy7k3v4s.bkt.clouddn.com/photo.jpg"
             alt="HongqinMa' Blog"/>
      </a>
    </div>
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">HongqinMa' Blog</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">Spell at all costs， to rush you in the future。</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            Categories
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            About
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            Tags
          </a>
        </li>
      

      
    </ul>
  

  
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="//schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                js 的函数
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">Posted on</span>
            <time itemprop="dateCreated" datetime="2014-05-24T23:18:22+08:00" content="2014-05-24">
              2014-05-24
            </time>
          </span>

          
            <span class="post-category" >
              &nbsp; | &nbsp;
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">In</span>
              
                <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
                  <a href="/categories/JavaScript/" itemprop="url" rel="index">
                    <span itemprop="name">JavaScript</span>
                  </a>
                </span>

                
                

              
            </span>
          

          
            
              <span class="post-comments-count">
                &nbsp; | &nbsp;
                <a href="/2014/05/24/10-js-的函数/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="2014/05/24/10-js-的函数/" itemprop="commentsCount"></span>
                </a>
              </span>
            
          

          

          

          
              &nbsp; | &nbsp;
              <span class="page-pv"><i class="fa fa-file-o"></i>
              <span class="busuanzi-value" id="busuanzi_value_page_pv" ></span>
              </span>
          

          
            <span id="busuanzi_container_page_pv">  <span id="busuanzi_value_page_pv"></span> Views</span>
          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>js &#x9AD8;&#x7A0B;&#x4E2D;&#x8FD9;&#x6837;&#x5B9A;&#x4E49;&#x51FD;&#x6570;&#xFF1A;&#x51FD;&#x6570;&#x662F;&#x8FD9;&#x6837;&#x4E00;&#x6BB5; JavaScript &#x4EE3;&#x7801;&#xFF0C;&#x53EA;&#x5B9A;&#x4E49;&#x4E00;&#x6B21;&#xFF0C;&#x4F46;&#x53EF;&#x4EE5;&#x88AB;&#x6267;&#x884C;&#x4EFB;&#x610F;&#x591A;&#x6B21;&#x3002;JavaScript &#x7684;&#x51FD;&#x6570;&#x662F;&#x53C2;&#x6570;&#x5316;&#x7684;&#xFF1A;&#x51FD;&#x6570;&#x7684;&#x5B9A;&#x4E49;&#x4F1A;&#x5305;&#x62EC;&#x4E00;&#x4E2A;&#x79F0;&#x4E3A;&#x5F62;&#x53C2;&#x548C;&#x6807;&#x8BC6;&#x7B26;&#x5217;&#x8868;&#xFF0C;&#x8FD9;&#x4E9B;&#x53C2;&#x6570;&#x5728;&#x51FD;&#x6570;&#x4F53;&#x4E2D;&#x5C31;&#x50CF;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x4E00;&#x6837;&#x5DE5;&#x4F5C;&#x3002;&#x51FD;&#x6570;&#x7684;&#x8C03;&#x7528;&#x4F1A;&#x4E3A;&#x5F62;&#x53C2;&#x63D0;&#x4F9B;&#x5B9E;&#x53C2;&#x7684;&#x503C;&#x3002;&#x51FD;&#x6570;&#x4F7F;&#x7528;&#x5B83;&#x4EEC;&#x5B9E;&#x53C2;&#x7684;&#x503C;&#x6765;&#x8BA1;&#x7B97;&#x8FD4;&#x56DE;&#x503C;&#xFF0C;&#x6210;&#x4E3A;&#x8BE5;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x503C;&#x3002;&#x51FA;&#x6765;&#x5B9E;&#x53C2;&#x4E4B;&#x5916;&#xFF0C;&#x6BCF;&#x6B21;&#x8C03;&#x7528;&#x8FD8;&#x4F1A;&#x62E5;&#x6709;&#x53E6;&#x4E00;&#x4E2A;&#x503C;-&#x672C;&#x6B21;&#x8C03;&#x7528;&#x7684;&#x4E0A;&#x4E0B;&#x6587;-&#x8FD9;&#x5C31;&#x662F; <code>this</code> &#x5173;&#x952E;&#x5B57;&#x7684;&#x503C;&#x3002;</p>
<p>&#x5982;&#x679C;&#x51FD;&#x6570;&#x6302;&#x8F7D;&#x5728;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x4E0A;&#xFF0C;&#x4F5C;&#x4E3A;&#x5BF9;&#x8C61;&#x7684;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;&#x8C03;&#x7528;&#xFF0C;&#x5C31;&#x79F0;&#x4E4B;&#x4E3A; <code>&#x5BF9;&#x8C61;&#x7684;&#x65B9;&#x6CD5;</code>&#x3002;&#x5F53;&#x901A;&#x8FC7;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x6765;&#x8C03;&#x7528;&#x51FD;&#x6570;&#x65F6;&#xFF0C;&#x8BE5;&#x5BF9;&#x8C61;&#x5C31;&#x662F;&#x6B64;&#x6B21;&#x8C03;&#x7528;&#x7684;&#x4E0A;&#x4E0B;&#x6587;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8BE5;&#x51FD;&#x6570;&#x7684; this &#x7684;&#x503C;&#x3002;</p>
<p>&#x7528;&#x4E8E;&#x521D;&#x59CB;&#x5316;&#x4E00;&#x4E2A;&#x65B0;&#x5EFA;&#x7684;&#x5BF9;&#x8C61;&#x7684;&#x51FD;&#x6570;&#x6210;&#x4E3A;<code>&#x6784;&#x9020;&#x51FD;&#x6570;</code>&#x3002;</p>
<p>&#x5728; JavaScript &#x91CC;&#xFF0C;&#x51FD;&#x6570;&#x5373;&#x5BF9;&#x8C61;&#x3002;&#x53EF;&#x4EE5;&#x628A;&#x51FD;&#x6570;&#x8D4B;&#x503C;&#x7ED9;&#x53D8;&#x91CF;&#xFF0C;&#x6216;&#x8005;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#x4F20;&#x9012;&#x7ED9;&#x5176;&#x4ED6;&#x51FD;&#x6570;&#x3002;</p>
<p>JavaScript &#x7684;&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x5D4C;&#x5957;&#x5728;&#x5176;&#x4ED6;&#x51FD;&#x6570;&#x4E2D;&#x5B9A;&#x4E49;&#xFF0C;&#x8FD9;&#x6837;&#x5C31;&#x53EF;&#x4EE5;&#x8BBF;&#x95EE;&#x5B83;&#x4EEC;&#x88AB;&#x5B9A;&#x4E49;&#x65F6;&#x6240;&#x5904;&#x7684;&#x4F5C;&#x7528;&#x57DF;&#x4E2D;&#x7684;&#x4EFB;&#x4F55;&#x53D8;&#x91CF;&#x3002;&#x8FD9;&#x610F;&#x5473;&#x7740; JavaScript &#x51FD;&#x6570;&#x6784;&#x6210;&#x4E86;&#x4E00;&#x4E2A;&#x95ED;&#x5305;&#x3002;</p>
<h2 id="&#x51FD;&#x6570;&#x7684;&#x5B9A;&#x4E49;"><a href="#&#x51FD;&#x6570;&#x7684;&#x5B9A;&#x4E49;" class="headerlink" title="&#x51FD;&#x6570;&#x7684;&#x5B9A;&#x4E49;"></a>&#x51FD;&#x6570;&#x7684;&#x5B9A;&#x4E49;</h2><h3 id="&#x51FD;&#x6570;&#x5B9A;&#x4E49;&#x65B9;&#x5F0F;&#x4E00;&#xFF1A;-&#x51FD;&#x6570;&#x58F0;&#x660E;"><a href="#&#x51FD;&#x6570;&#x5B9A;&#x4E49;&#x65B9;&#x5F0F;&#x4E00;&#xFF1A;-&#x51FD;&#x6570;&#x58F0;&#x660E;" class="headerlink" title="&#x51FD;&#x6570;&#x5B9A;&#x4E49;&#x65B9;&#x5F0F;&#x4E00;&#xFF1A; &#x51FD;&#x6570;&#x58F0;&#x660E;"></a>&#x51FD;&#x6570;&#x5B9A;&#x4E49;&#x65B9;&#x5F0F;&#x4E00;&#xFF1A; &#x51FD;&#x6570;&#x58F0;&#x660E;</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">funcName</span>(<span class="params">arg0, arg1, ...</span>)</span>{</div><div class="line">  <span class="comment">// &#x51FD;&#x6570;&#x4F53;</span></div><div class="line">}</div></pre></td></tr></table></figure>
<p>&#x51FD;&#x6570;&#x7684;&#x7EC4;&#x6210;&#xFF1A; </p>
<ul>
<li>function &#x5173;&#x952E;&#x5B57;</li>
<li>&#x51FD;&#x6570;&#x540D;&#x6807;&#x8BC6;&#x7B26;&#xFF1A;&#x662F;&#x51FD;&#x6570;&#x58F0;&#x660E;&#x8BED;&#x53E5;&#x7684;&#x5FC5;&#x8981;&#x7EC4;&#x6210;</li>
<li>&#x4E00;&#x5BF9;&#x5706;&#x62EC;&#x53F7;&#xFF1A;&#x5176;&#x4E2D;&#x5305;&#x542B;&#x6709; 0 &#x4E2A;&#x6216;&#x591A;&#x4E2A;&#x9017;&#x53F7;&#x5206;&#x9694;&#x7684;&#x6807;&#x8BC6;&#x7B26;&#x7EC4;&#x6210;&#x7684;&#x5217;&#x8868;&#xFF0C;&#x8868;&#x793A;&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;</li>
<li>&#x4E00;&#x5BF9;&#x82B1;&#x62EC;&#x53F7;</li>
</ul>
<h3 id="&#x51FD;&#x6570;&#x7684;&#x5B9A;&#x4E49;&#x65B9;&#x5F0F;&#x4E8C;&#xFF1A;-&#x51FD;&#x6570;&#x8868;&#x8FBE;&#x5F0F;"><a href="#&#x51FD;&#x6570;&#x7684;&#x5B9A;&#x4E49;&#x65B9;&#x5F0F;&#x4E8C;&#xFF1A;-&#x51FD;&#x6570;&#x8868;&#x8FBE;&#x5F0F;" class="headerlink" title="&#x51FD;&#x6570;&#x7684;&#x5B9A;&#x4E49;&#x65B9;&#x5F0F;&#x4E8C;&#xFF1A; &#x51FD;&#x6570;&#x8868;&#x8FBE;&#x5F0F;"></a>&#x51FD;&#x6570;&#x7684;&#x5B9A;&#x4E49;&#x65B9;&#x5F0F;&#x4E8C;&#xFF1A; &#x51FD;&#x6570;&#x8868;&#x8FBE;&#x5F0F;</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> functionName = <span class="function"><span class="keyword">function</span>(<span class="params">arg0, arg1, ...</span>) </span>{</div><div class="line">  <span class="comment">//&#x51FD;&#x6570;&#x4F53;</span></div><div class="line">} </div><div class="line"></div><div class="line"><span class="keyword">var</span> functionName = <span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params">arg0, arg1, ...</span>) </span>{</div><div class="line">  <span class="comment">//&#x51FD;&#x6570;&#x4F53;</span></div><div class="line">}</div></pre></td></tr></table></figure>
<p>&#x4EE5;&#x51FD;&#x6570;&#x8868;&#x8FBE;&#x5F0F;&#x5B9A;&#x4E49;&#x7684;&#x51FD;&#x6570;&#xFF0C;&#x51FD;&#x6570;&#x540D;&#x79F0;&#x662F;&#x53EF;&#x9009;&#x7684;&#x3002;&#x4E00;&#x6761;&#x8BED;&#x53E5;&#x5B9E;&#x9645;&#x4E0A;&#x58F0;&#x660E;&#x4E86;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#xFF0C;&#x5E76;&#x628A;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x5BF9;&#x8C61;&#x8D4B;&#x503C;&#x7ED9;&#x5B83;&#x3002;&#x51FD;&#x6570;&#x8868;&#x8FBE;&#x5F0F;&#x5B9A;&#x4E49;&#x51FD;&#x6570;&#x901A;&#x5E38;&#x4E0D;&#x52A0;&#x51FD;&#x6570;&#x540D;&#xFF0C;&#x56FE;&#x7279;&#x522B;&#x9002;&#x5408;&#x4EC5;&#x8C03;&#x7528;&#x4E00;&#x6B21;&#x7684;&#x51FD;&#x6570;&#x3002;</p>
<h3 id="&#x51FD;&#x6570;&#x7684;&#x5D4C;&#x5957;"><a href="#&#x51FD;&#x6570;&#x7684;&#x5D4C;&#x5957;" class="headerlink" title="&#x51FD;&#x6570;&#x7684;&#x5D4C;&#x5957;"></a>&#x51FD;&#x6570;&#x7684;&#x5D4C;&#x5957;</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">hypotenuse</span>(<span class="params">a, b</span>) </span>{</div><div class="line">  <span class="function"><span class="keyword">function</span> <span class="title">square</span>(<span class="params">x</span>) </span>{ <span class="keyword">return</span> x * x; }</div><div class="line">  <span class="keyword">return</span> <span class="built_in">Math</span>.sqrt(square(a) + square(b));</div><div class="line">}</div></pre></td></tr></table></figure>
<h2 id="&#x51FD;&#x6570;&#x8C03;&#x7528;&#x3001;this&#x6307;&#x5411;&#x3001;&#x8FD4;&#x56DE;&#x503C;"><a href="#&#x51FD;&#x6570;&#x8C03;&#x7528;&#x3001;this&#x6307;&#x5411;&#x3001;&#x8FD4;&#x56DE;&#x503C;" class="headerlink" title="&#x51FD;&#x6570;&#x8C03;&#x7528;&#x3001;this&#x6307;&#x5411;&#x3001;&#x8FD4;&#x56DE;&#x503C;"></a>&#x51FD;&#x6570;&#x8C03;&#x7528;&#x3001;this&#x6307;&#x5411;&#x3001;&#x8FD4;&#x56DE;&#x503C;</h2><p><strong>&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x6700;&#x7EC8;&#x4EA7;&#x751F;&#x4EC0;&#x4E48;&#x6837;&#x7684;&#x7ED3;&#x6784;&#xFF0C;&#x8DDF;&#x5982;&#x4F55;&#x8C03;&#x7528;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x606F;&#x606F;&#x76F8;&#x5173;&#xFF1A;&#x51FD;&#x6570;&#x7684;&#x56DB;&#x79CD;&#x8C03;&#x7528;&#x6A21;&#x5F0F;</strong></p>
<h3 id="&#x51FD;&#x6570;&#x7684;4&#x79CD;&#x8C03;&#x7528;&#x6A21;&#x5F0F;"><a href="#&#x51FD;&#x6570;&#x7684;4&#x79CD;&#x8C03;&#x7528;&#x6A21;&#x5F0F;" class="headerlink" title="&#x51FD;&#x6570;&#x7684;4&#x79CD;&#x8C03;&#x7528;&#x6A21;&#x5F0F;"></a>&#x51FD;&#x6570;&#x7684;4&#x79CD;&#x8C03;&#x7528;&#x6A21;&#x5F0F;</h3><ol>
<li>&#x7B2C;&#x4E00;&#x79CD;&#x6A21;&#x5F0F;&#xFF1A;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x6A21;&#x5F0F;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x5199;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x7136;&#x540E;&#x8C03;&#x7528;&#x4E00;&#x4E0B;</li>
<li>&#x7B2C;&#x4E8C;&#x79CD;&#x6A21;&#x5F0F;&#xFF1A;&#x65B9;&#x6CD5;&#x8C03;&#x7528;&#x6A21;&#x5F0F;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x5C06;&#x51FD;&#x6570;&#x6210;&#x4E3A;&#x5BF9;&#x8C61;&#x7684;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;&#xFF0C;&#x7136;&#x540E;&#x901A;&#x8FC7;&#x5BF9;&#x8C61;&#x6765;&#x8C03;&#x7528;</li>
<li>&#x7B2C;&#x4E09;&#x79CD;&#x6A21;&#x5F0F;&#xFF1A;&#x6784;&#x9020;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x6A21;&#x5F0F;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x5C06;&#x51FD;&#x6570;&#x5F53;&#x6210;&#x6784;&#x9020;&#x51FD;&#x6570;&#x6765;&#x8C03;&#x7528;</li>
<li>&#x7B2C;&#x56DB;&#x79CD;&#x8C03;&#x7528;&#x6A21;&#x5F0F;&#xFF1A;&#x4E0A;&#x4E0B;&#x6587;&#x8C03;&#x7528;&#x6A21;&#x5F0F;&#xFF0C;&#x6839;&#x636E;&#x8C03;&#x7528;&#x65B9;&#x5F0F;&#x7684;&#x4E0D;&#x540C;&#x53EF;&#x4EE5;&#x4EA7;&#x751F;&#x4E0D;&#x540C;&#x7684;&#x7ED3;&#x679C;</li>
</ol>
<p>&#x7B2C;&#x56DB;&#x79CD;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x7684;&#x5B9E;&#x73B0;&#x65B9;&#x5F0F;</p>
<ul>
<li>&#x5B9E;&#x73B0;&#x65B9;&#x5F0F;&#xFF1A;call/apply (apply &#x548C; call &#x7684;&#x552F;&#x4E00;&#x533A;&#x522B;&#x662F;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x662F;&#x6570;&#x7EC4;&#xFF0C;&#x5C06;&#x5B9E;&#x53C2;&#x503C;&#x4E00;&#x4E00;&#x4F20;&#x5230;&#x6570;&#x7EC4;&#x4E2D;&#x3002;fn.call (&#x51FD;&#x6570;&#x5185;&#x90E8;&#x7684; this &#x7684;&#x503C;,&#x5B9E;&#x53C2;1&#xFF0C;&#x5B9E;&#x53C2;2&#x2026;))</li>
</ul>
<h3 id="&#x4E0D;&#x540C;&#x8C03;&#x7528;&#x6A21;&#x5F0F;&#x4E2D;&#x7684;this&#x7684;&#x503C;"><a href="#&#x4E0D;&#x540C;&#x8C03;&#x7528;&#x6A21;&#x5F0F;&#x4E2D;&#x7684;this&#x7684;&#x503C;" class="headerlink" title="&#x4E0D;&#x540C;&#x8C03;&#x7528;&#x6A21;&#x5F0F;&#x4E2D;&#x7684;this&#x7684;&#x503C;"></a>&#x4E0D;&#x540C;&#x8C03;&#x7528;&#x6A21;&#x5F0F;&#x4E2D;&#x7684;this&#x7684;&#x503C;</h3><ol>
<li>&#x51FD;&#x6570;&#x8C03;&#x7528;&#x6A21;&#x5F0F;&#x4E2D; this &#x6307;&#x5411;&#xFF1A;window</li>
<li>&#x65B9;&#x6CD5;&#x8C03;&#x7528;&#x6A21;&#x5F0F;&#x4E2D; this &#x6307;&#x5411;&#xFF1A;&#x8C03;&#x7528;&#x7684;&#x5BF9;&#x8C61;</li>
<li>&#x6784;&#x9020;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x6A21;&#x5F0F;&#x4E2D; this &#x6307;&#x5411;&#xFF1A;&#x6784;&#x9020;&#x51FD;&#x6570;&#x7684;&#x5B9E;&#x4F8B;</li>
<li>&#x4E0A;&#x4E0B;&#x6587;&#x8C03;&#x7528;&#x6A21;&#x5F0F;&#x4E2D; this &#x6307;&#x5411;&#xFF1A;</li>
</ol>
<ul>
<li>(1) &#x5982;&#x679C; call &#x65B9;&#x6CD5;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x662F;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#xFF0C;&#x5219; fn &#x51FD;&#x6570;&#x5185;&#x90E8;&#x7684; this &#x7684;&#x503C;&#x6307;&#x5411;&#x8BE5;&#x5BF9;&#x8C61;</li>
<li>(2) &#x5982;&#x679C; call &#x65B9;&#x6CD5;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x662F;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#x3001;&#x6570;&#x5B57;&#x3001;&#x5E03;&#x5C14;&#x503C;&#xFF0C;&#x5219; fn &#x51FD;&#x6570;&#x5185;&#x90E8;&#x7684; this &#x7684;&#x503C;&#x4F1A;&#x8F6C;&#x6362;&#x4E3A;&#x8BE5;&#x7C7B;&#x578B;&#x6240;&#x5BF9;&#x5E94;&#x7684;&#x57FA;&#x672C;&#x5305;&#x88C5;&#x7C7B;&#x578B;&#x7684;&#x5BF9;&#x8C61;</li>
<li>(3) &#x5982;&#x679C; call &#x65B9;&#x6CD5;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x662F; null &#xFF0C;&#x5219; fn &#x51FD;&#x6570;&#x5185;&#x90E8;&#x7684; this &#x7684;&#x503C;&#x662F; window &#x2014;&#x2014;&gt; &#x5C31;&#x76F8;&#x5F53;&#x4E8E;&#x662F;&#x4E00;&#x6B21;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x6A21;&#x5F0F;</li>
</ul>
<h3 id="&#x8C03;&#x7528;&#x6A21;&#x5F0F;&#x4E2D;&#x7684;&#x8FD4;&#x56DE;&#x503C;"><a href="#&#x8C03;&#x7528;&#x6A21;&#x5F0F;&#x4E2D;&#x7684;&#x8FD4;&#x56DE;&#x503C;" class="headerlink" title="&#x8C03;&#x7528;&#x6A21;&#x5F0F;&#x4E2D;&#x7684;&#x8FD4;&#x56DE;&#x503C;"></a>&#x8C03;&#x7528;&#x6A21;&#x5F0F;&#x4E2D;&#x7684;&#x8FD4;&#x56DE;&#x503C;</h3><ol>
<li>&#x51FD;&#x6570;&#x8C03;&#x7528;&#x6A21;&#x5F0F;&#x4E2D;&#x8FD4;&#x56DE;&#x503C;&#xFF1A;&#x7531; return &#x8BED;&#x53E5;&#x51B3;&#x5B9A;</li>
<li>&#x65B9;&#x6CD5;&#x8C03;&#x7528;&#x6A21;&#x5F0F;&#x4E2D;&#x8FD4;&#x56DE;&#x503C;&#xFF1A;&#x7531; return &#x8BED;&#x53E5;&#x51B3;&#x5B9A;</li>
<li>&#x6784;&#x9020;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x6A21;&#x5F0F;&#x4E2D;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#xFF1A;</li>
</ol>
<ul>
<li>(1). &#x5982;&#x679C;&#x6784;&#x9020;&#x51FD;&#x6570;&#x6CA1;&#x6709;&#x624B;&#x52A8;&#x8BBE;&#x7F6E;&#x8FD4;&#x56DE;&#x503C;&#xFF0C;&#x90A3;&#x4E48;&#x4F1A;&#x8FD4;&#x56DE;&#x6784;&#x9020;&#x51FD;&#x6570;&#x7684;&#x5B9E;&#x4F8B;</li>
<li>(2). &#x5982;&#x679C;&#x624B;&#x52A8;&#x7ED9;&#x6784;&#x9020;&#x51FD;&#x6570;&#x6DFB;&#x52A0;&#x4E86;&#x8FD4;&#x56DE;&#x503C;&#xFF0C;&#x6709;&#x4EE5;&#x4E0B;2&#x79CD;&#x60C5;&#x51B5;&#xFF1A;<ul>
<li>(a). &#x8FD4;&#x56DE;&#x503C;&#x662F;&#x503C;&#x7C7B;&#x578B;&#xFF1A;&#x6700;&#x7EC8;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x8FD8;&#x662F;&#x6784;&#x9020;&#x51FD;&#x6570;&#x7684;&#x5B9E;&#x4F8B;</li>
<li>(b). &#x8FD4;&#x56DE;&#x503C;&#x662F;&#x5F15;&#x7528;&#x7C7B;&#x578B;(&#x5BF9;&#x8C61;)&#xFF1A;&#x6700;&#x7EC8;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x5C31;&#x662F;&#x8BE5;&#x5BF9;&#x8C61;</li>
</ul>
</li>
</ul>
<ol>
<li>&#x4E0A;&#x4E0B;&#x6587;&#x8C03;&#x7528;&#x6A21;&#x5F0F;&#x4E2D;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#xFF1A;&#x7531; return &#x8BED;&#x53E5;&#x51B3;&#x5B9A;</li>
</ol>
<h2 id="&#x51FD;&#x6570;&#x7684;&#x5B9E;&#x53C2;&#x548C;&#x5F62;&#x53C2;"><a href="#&#x51FD;&#x6570;&#x7684;&#x5B9E;&#x53C2;&#x548C;&#x5F62;&#x53C2;" class="headerlink" title="&#x51FD;&#x6570;&#x7684;&#x5B9E;&#x53C2;&#x548C;&#x5F62;&#x53C2;"></a>&#x51FD;&#x6570;&#x7684;&#x5B9E;&#x53C2;&#x548C;&#x5F62;&#x53C2;</h2><h3 id="&#x51FD;&#x6570;&#x7684;&#x53EF;&#x9009;&#x5F62;&#x53C2;"><a href="#&#x51FD;&#x6570;&#x7684;&#x53EF;&#x9009;&#x5F62;&#x53C2;" class="headerlink" title="&#x51FD;&#x6570;&#x7684;&#x53EF;&#x9009;&#x5F62;&#x53C2;"></a>&#x51FD;&#x6570;&#x7684;&#x53EF;&#x9009;&#x5F62;&#x53C2;</h3><p><strong>&#x5F53;&#x8C03;&#x7528;&#x51FD;&#x6570;&#x7684;&#x65F6;&#x5019;&#x4F20;&#x5165;&#x7684;&#x5B9E;&#x53C2;&#x6BD4;&#x51FD;&#x6570;&#x58F0;&#x660E;&#x65F6;&#x6307;&#x5B9A;&#x7684;&#x5F62;&#x53C2;&#x4E2A;&#x6570;&#x8981;&#x5C11;&#xFF0C;&#x5269;&#x4E0B;&#x7684;&#x5F62;&#x53C2;&#x90FD;&#x5C06;&#x8BBE;&#x7F6E;&#x4E3A; undefined &#x503C;&#x3002;</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">getPropertyName</span>(<span class="params">o, <span class="regexp">/*&#x53EF;&#x9009;&#x7684;*/</span> a</span>) </span>{</div><div class="line">  <span class="keyword">if</span> (a === <span class="literal">undefined</span>) a = [];  <span class="comment">// &#x5982;&#x679C;&#x672A;&#x5B9A;&#x4E49;&#xFF0C;&#x8D4B;&#x503C;&#x4E00;&#x4E2A;&#x65B0;&#x6570;&#x7EC4;</span></div><div class="line">  <span class="comment">// &#x4E0A;&#x9762;&#x8FD9;&#x53E5;&#x4EE3;&#x7801;&#x53EF;&#x4EE5;&#x66FF;&#x6362;&#x6210; a = a || []; // &#x8FD9;&#x624D;&#x662F;&#x4E60;&#x60EF;&#x5199;&#x6CD5;</span></div><div class="line">  <span class="keyword">for</span> (<span class="keyword">var</span> property <span class="keyword">in</span> a) a.push(property);</div><div class="line">  <span class="keyword">return</span> a;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">var</span> a = getPropertyName(o);</div><div class="line">getPropertyNames(o, p); <span class="comment">// &#x5C06; &#xFF50; &#x7684;&#x5C5E;&#x6027;&#x8FFD;&#x52A0;&#x5230;&#x6570;&#x7EC4; a &#x4E2D;</span></div></pre></td></tr></table></figure>
<p>&#x6CE8;&#x610F;&#xFF1A; &#x9700;&#x8981;&#x5B9A;&#x4E49;&#x53EF;&#x9009;&#x7684;&#x5B9E;&#x53C2;&#x6765;&#x5B9E;&#x73B0;&#x51FD;&#x6570;&#x65F6;&#xFF0C;&#x9700;&#x8981;&#x5C06;&#x53EF;&#x9009;&#x7684;&#x5B9E;&#x53C2;&#x653E;&#x5728;&#x5B9E;&#x53C2;&#x5217;&#x8868;&#x7684;&#x6700;&#x540E;&#x3002;</p>
<h3 id="&#x53EF;&#x53D8;&#x957F;&#x7684;&#x5B9E;&#x53C2;&#x5217;&#x8868;&#x3001;&#x5B9E;&#x53C2;&#x5BF9;&#x8C61;"><a href="#&#x53EF;&#x53D8;&#x957F;&#x7684;&#x5B9E;&#x53C2;&#x5217;&#x8868;&#x3001;&#x5B9E;&#x53C2;&#x5BF9;&#x8C61;" class="headerlink" title="&#x53EF;&#x53D8;&#x957F;&#x7684;&#x5B9E;&#x53C2;&#x5217;&#x8868;&#x3001;&#x5B9E;&#x53C2;&#x5BF9;&#x8C61;"></a>&#x53EF;&#x53D8;&#x957F;&#x7684;&#x5B9E;&#x53C2;&#x5217;&#x8868;&#x3001;&#x5B9E;&#x53C2;&#x5BF9;&#x8C61;</h3><p>&#x5F53;&#x8C03;&#x7528;&#x51FD;&#x6570;&#x65F6;&#x4F20;&#x5165;&#x7684;&#x5B9E;&#x53C2;&#x4E2A;&#x6570;&#x8D85;&#x8FC7;&#x51FD;&#x6570;&#x5B9A;&#x4E49;&#x65F6;&#x7684;&#x5F62;&#x53C2;&#x4E2A;&#x6570;&#x65F6;&#xFF0C;&#x6CA1;&#x6709;&#x529E;&#x6CD5;&#x76F4;&#x63A5;&#x83B7;&#x5F97;&#x672A;&#x547D;&#x540D;&#x503C;&#x7684;&#x5F15;&#x7528;&#x3002;&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;&#x5BF9;&#x8C61;&#x89E3;&#x51B3;&#x4E86;&#x8FD9;&#x4E2A;&#x95EE;&#x9898;&#x3002;</p>
<p>&#x5728;&#x51FD;&#x6570;&#x4F53;&#x5185;&#x90E8;&#xFF0C; &#x6807;&#x8BC6;&#x7B26; arguments &#x6307;&#x5411;&#x5B9E;&#x53C2;&#x5BF9;&#x8C61;&#x7684;&#x5F15;&#x7528;&#xFF0C;&#x5B9E;&#x53C2;&#x5BF9;&#x8C61;&#x5305;&#x542B;&#x4E00;&#x4E2A; length &#x5C5E;&#x6027;&#xFF0C;&#x662F;&#x4E00;&#x4E2A;&#x4F2A;&#x6570;&#x7EC4;&#x3002;&#x3001;</p>
<p>&#x5B9E;&#x53C2;&#x7684;&#x91CD;&#x8981;&#x7528;&#x9014;&#x662F;&#x53EF;&#x4EE5;&#x64CD;&#x4F5C;&#x4EFB;&#x610F;&#x6570;&#x91CF;&#x7684;&#x5B9E;&#x53C2;&#x3002;</p>
<p>&#x5728;&#x975E;&#x4E25;&#x683C;&#x6A21;&#x5F0F;&#x4E0B;&#xFF0C;&#x5F53;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x5305;&#x542B;&#x82E5;&#x5E72;&#x4E2A;&#x5F62;&#x53C2;&#xFF0C;&#x5B9E;&#x53C2;&#x5BF9;&#x8C61;&#x7684;&#x6570;&#x7EC4;&#x5143;&#x7D20;&#x662F;&#x51FD;&#x6570;&#x5F62;&#x53C2;&#x6240;&#x5BF9;&#x5E94;&#x5B9E;&#x53C2;&#x7684;&#x522B;&#x540D;&#xFF0C;&#x5B9E;&#x53C2;&#x5BF9;&#x8C61;&#x4E2D;&#x4EE5;&#x6570;&#x5B57;&#x7D22;&#x5F15;&#xFF0C;&#x5E76;&#x4E14;&#x5F62;&#x53C2;&#x540D;&#x79F0;&#x53EF;&#x4EE5;&#x8BA4;&#x4E3A;&#x662F;&#x76F8;&#x540C;&#x53D8;&#x91CF;&#x7684;&#x4E0D;&#x540C;&#x547D;&#x540D;&#x3002;</p>
<p>&#x4E25;&#x683C;&#x6A21;&#x5F0F;&#x4E0B;&#xFF0C;arguments &#x5BF9;&#x8C61;&#x53D8;&#x6210;&#x4E86;&#x4E00;&#x4E2A;&#x4FDD;&#x7559;&#x5B57;&#xFF0C;&#x4E0D;&#x80FD;&#x7ED9;&#x5176;&#x8D4B;&#x503C;&#xFF0C;&#x4E5F;&#x4E0D;&#x80FD;&#x4F7F;&#x7528; arguments &#x4F5C;&#x4E3A;&#x5F62;&#x53C2;&#x540D;&#x6216;&#x8005;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x540D;&#xFF0C;&#x4E5F;&#x4E0D;&#x80FD;&#x7ED9; arguments &#x8D4B;&#x503C;&#x3002;</p>
<h3 id="callee-&#x548C;-caller-&#x5C5E;&#x6027;"><a href="#callee-&#x548C;-caller-&#x5C5E;&#x6027;" class="headerlink" title="callee &#x548C; caller &#x5C5E;&#x6027;"></a>callee &#x548C; caller &#x5C5E;&#x6027;</h3><p>callee &#x6307;&#x5411;&#x5F53;&#x524D;&#x6B63;&#x5728;&#x6267;&#x884C;&#x7684;&#x51FD;&#x6570;&#x3002;</p>
<p>caller &#x662F;&#x975E;&#x6807;&#x51C6;&#x7684;&#xFF0C;&#x4F46;&#x5927;&#x591A;&#x6570;&#x51FD;&#x6570;&#x5B9E;&#x73B0;&#x4E86;&#x8FD9;&#x4E2A;&#x5C5E;&#x6027;&#x3002;&#x6307;&#x7684;&#x662F;&#x8C03;&#x7528;&#x5F53;&#x524D;&#x6B63;&#x5728;&#x6267;&#x884C;&#x7684;&#x51FD;&#x6570;&#x7684;&#x51FD;&#x6570;&#x3002;</p>
<h3 id="&#x5C06;&#x5BF9;&#x8C61;&#x7684;&#x5C5E;&#x6027;&#x4F5C;&#x4E3A;&#x5B9E;&#x53C2;"><a href="#&#x5C06;&#x5BF9;&#x8C61;&#x7684;&#x5C5E;&#x6027;&#x4F5C;&#x4E3A;&#x5B9E;&#x53C2;" class="headerlink" title="&#x5C06;&#x5BF9;&#x8C61;&#x7684;&#x5C5E;&#x6027;&#x4F5C;&#x4E3A;&#x5B9E;&#x53C2;"></a>&#x5C06;&#x5BF9;&#x8C61;&#x7684;&#x5C5E;&#x6027;&#x4F5C;&#x4E3A;&#x5B9E;&#x53C2;</h3><p>JavaScript &#x4E2D;&#xFF0C;&#x53EF;&#x4EE5;&#x901A;&#x8FC7; <code>&#x952E;/&#x503C;</code> &#x5BF9;&#x7684;&#x5F62;&#x5F0F;&#x6765;&#x4F20;&#x5165;&#x53C2;&#x6570;&#xFF0C;&#x8FD9;&#x6837;&#x5F53;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x4E2D;&#x7684;&#x53C2;&#x6570;&#x6709;&#x5F88;&#x591A;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x4E0D;&#x9700;&#x8981;&#x8BB0;&#x4F4F;&#x4F20;&#x5165;&#x7684;&#x987A;&#x5E8F;&#x3002;</p>
<p>&#x8FD9;&#x79CD;&#x98CE;&#x683C;&#x8C03;&#x7528;&#x7684;&#x51FD;&#x6570;&#xFF0C;&#x4F20;&#x5165;&#x7684;&#x5B9E;&#x53C2;&#x90FD;&#x5199;&#x8FDB;&#x4E00;&#x4E2A;&#x5355;&#x72EC;&#x7684;&#x5BF9;&#x8C61;&#x4E2D;&#xFF0C;&#x5728;&#x8C03;&#x7528;&#x7684;&#x65F6;&#x5019;&#x4F20;&#x5165;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#xFF0C;&#x5BF9;&#x8C61;&#x4E2D;&#x7684; <code>&#x952E;/&#x503C;</code> &#x5BF9;&#x662F;&#x771F;&#x6B63;&#x9700;&#x8981;&#x7684;&#x5B9E;&#x53C2;&#x6570;&#x636E;&#x3002;&#x5982;&#xFF1A;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// &#x5C06;&#x539F;&#x59CB;&#x6570;&#x7EC4;&#x4E2D;&#x7684; length &#x5143;&#x7D20;&#x590D;&#x5236;&#x5230;&#x76EE;&#x6807;&#x6570;&#x7EC4;&#x4E2D;</span></div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">arraycopy</span>(<span class="params"><span class="regexp">/* array */</span> from, </span></span></div><div class="line">    <span class="regexp">/* index */</span> form_start, </div><div class="line">    <span class="regexp">/* array */</span> to, </div><div class="line">    <span class="regexp">/* index */</span> to_start, </div><div class="line">    <span class="regexp">/* integer */</span> length){</div><div class="line">    <span class="comment">// &#x4EE3;&#x7801;&#x6BB5;</span></div><div class="line">};</div><div class="line"><span class="comment">// &#x8FD9;&#x79CD;&#x65B9;&#x5F0F;&#x6548;&#x7387;&#x8F83;&#x4F4E;&#xFF0C;&#x4F46;&#x4E0D;&#x5FC5;&#x8BB0;&#x4F4F;&#x53C2;&#x6570;&#x987A;&#x5E8F;</span></div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">easycopy</span>(<span class="params">args</span>) </span>{</div><div class="line">    arraycopy(args.from,</div><div class="line">        args.from_start || <span class="number">0</span>,</div><div class="line">        args.to,</div><div class="line">        args.to_start || <span class="number">0</span>,</div><div class="line">        args.length );</div><div class="line">}</div><div class="line"><span class="comment">// &#x8C03;&#x7528; easycopy &#x65B9;&#x6CD5;</span></div><div class="line"><span class="keyword">var</span> a = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>], b = [];</div><div class="line">easycopy({<span class="attr">from</span>: a, <span class="attr">to</span>: b, <span class="attr">length</span>: <span class="number">4</span>});</div></pre></td></tr></table></figure>
<p>&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x4EE5;&#x4E0A;&#x8FD9;&#x79CD;&#x4EE3;&#x7801;&#x6765;&#x9002;&#x5F53;&#x4F7F;&#x7528;&#x6587;&#x6863;&#x8BF4;&#x660E;&#x81EA;&#x5DF1;&#x7684;&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;</p>
<h3 id="&#x5B9E;&#x53C2;&#x7C7B;&#x578B;"><a href="#&#x5B9E;&#x53C2;&#x7C7B;&#x578B;" class="headerlink" title="&#x5B9E;&#x53C2;&#x7C7B;&#x578B;"></a>&#x5B9E;&#x53C2;&#x7C7B;&#x578B;</h3><p>JavaScript &#x65B9;&#x6CD5;&#x7684;&#x5F62;&#x53C2;&#x5E76;&#x672A;&#x58F0;&#x660E;&#x7C7B;&#x578B;&#xFF0C;&#x5728;&#x5F62;&#x53C2;&#x4F20;&#x5165;&#x51FD;&#x6570;&#x4E4B;&#x524D;&#x6CA1;&#x6709;&#x505A;&#x4EFB;&#x4F55;&#x7C7B;&#x578B;&#x68C0;&#x6D4B;&#x3002;&#x5728;&#x5B9A;&#x4E49;&#x51FD;&#x6570;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x9700;&#x8981;&#x6DFB;&#x52A0;&#x7C7B;&#x578B;&#x5224;&#x65AD;&#x3002;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">isArrayLike</span>(<span class="params">o</span>) </span>{</div><div class="line">    <span class="keyword">if</span> (o &amp;&amp; <span class="keyword">typeof</span> o === <span class="string">&quot;object&quot;</span> </div><div class="line">    &amp;&amp; <span class="built_in">isFinite</span>(o.length)</div><div class="line">    &amp;&amp; o.length &gt;=<span class="number">0</span> </div><div class="line">    &amp;&amp; o.length === <span class="built_in">Math</span>.floor(o.length)</div><div class="line">    &amp;&amp; o.length &lt; <span class="number">4294967296</span>)</div><div class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</div><div class="line">    <span class="keyword">else</span> </div><div class="line">        <span class="keyword">return</span> <span class="literal">false</span>;</div><div class="line">}</div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">sum</span>(<span class="params">a</span>) </span>{</div><div class="line">    <span class="keyword">var</span> total = <span class="number">0</span>;</div><div class="line">    <span class="keyword">for</span>(<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; argument.length; i++) {</div><div class="line">        <span class="keyword">var</span> element = <span class="built_in">arguments</span>[i], n;</div><div class="line">        <span class="keyword">if</span> (element == <span class="literal">null</span>) <span class="keyword">continue</span>;          <span class="comment">// &#x5FFD;&#x7565; null &#x548C; undefined &#x5B9E;&#x53C2;</span></div><div class="line">        <span class="keyword">if</span> (isArray(element))                   <span class="comment">// &#x5982;&#x679C;&#x5B9E;&#x53C2;&#x662F;&#x6570;&#x7EC4;</span></div><div class="line">            n = sum.apply(<span class="keyword">this</span>, element);       <span class="comment">// &#x9012;&#x5F52;&#x5730;&#x8BA1;&#x7B97;&#x7D2F;&#x52A0;...</span></div><div class="line">        <span class="keyword">else</span> <span class="keyword">if</span> (<span class="keyword">typeof</span> element === <span class="string">&apos;function&apos;</span>) <span class="comment">// &#x662F;&#x51FD;&#x6570;</span></div><div class="line">            n = <span class="built_in">Number</span>(element());              <span class="comment">// &#x8C03;&#x7528;&#x5E76;&#x505A;&#x7C7B;&#x578B;&#x8F6C;&#x6362;</span></div><div class="line">        <span class="keyword">else</span> </div><div class="line">            n = <span class="built_in">Number</span>(element);                <span class="comment">// &#x5426;&#x5219;&#x76F4;&#x63A5;&#x505A;&#x7C7B;&#x578B;&#x8F6C;&#x6362;         </span></div><div class="line">        <span class="keyword">if</span> (<span class="built_in">isNaN</span>(n))                           <span class="comment">// &#x5982;&#x679C;&#x65E0;&#x6CD5;&#x8F6C;&#x6362;&#x4E3A;&#x6570;&#x5B57;&#xFF0C;&#x629B;&#x51FA;&#x5F02;&#x5E38;</span></div><div class="line">            <span class="keyword">throw</span> <span class="built_in">Error</span>(<span class="string">&quot;sum(): can&apos;t convert &quot;</span> + element + <span class="string">&quot; to number&quot;</span>);</div><div class="line">        total += n; </div><div class="line">    }</div><div class="line">    <span class="keyword">return</span> total;</div><div class="line">}</div></pre></td></tr></table></figure>
<h2 id="&#x4F5C;&#x4E3A;&#x503C;&#x7684;&#x51FD;&#x6570;"><a href="#&#x4F5C;&#x4E3A;&#x503C;&#x7684;&#x51FD;&#x6570;" class="headerlink" title="&#x4F5C;&#x4E3A;&#x503C;&#x7684;&#x51FD;&#x6570;"></a>&#x4F5C;&#x4E3A;&#x503C;&#x7684;&#x51FD;&#x6570;</h2><p>JavaScript &#x4E2D;&#x7684;&#x51FD;&#x6570;&#x4E0D;&#x4EC5;&#x4EC5;&#x662F;&#x4E00;&#x79CD;&#x8BED;&#x6CD5;&#xFF0C;&#x4E5F;&#x662F;&#x503C;&#xFF0C;&#x53EF;&#x4EE5;&#x5C06;&#x51FD;&#x6570;&#x8D4B;&#x503C;&#x7ED9;&#x53D8;&#x91CF;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x5B58;&#x50A8;&#x5728;&#x5BF9;&#x8C61;&#x7684;&#x5C5E;&#x6027;&#x6216;&#x6570;&#x7EC4;&#x7684;&#x5143;&#x7D20;&#x4E2D;&#xFF0C;&#x8FD8;&#x53EF;&#x4EE5;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#x4F20;&#x5165;&#x53E6;&#x5916;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x7B49;&#x3002;</p>
<p>&#x6BD4;&#x5982;&#xFF1A;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> a = [<span class="function"><span class="keyword">function</span>(<span class="params">x</span>) </span>{ <span class="keyword">return</span> x*x;}, <span class="number">20</span>];</div><div class="line"><span class="built_in">console</span>.log(a[<span class="number">0</span>](a[<span class="number">2</span>]));  <span class="comment">// =&gt; 400</span></div></pre></td></tr></table></figure>
<h3 id="&#x81EA;&#x5B9A;&#x4E49;&#x51FD;&#x6570;&#x7684;&#x5C5E;&#x6027;"><a href="#&#x81EA;&#x5B9A;&#x4E49;&#x51FD;&#x6570;&#x7684;&#x5C5E;&#x6027;" class="headerlink" title="&#x81EA;&#x5B9A;&#x4E49;&#x51FD;&#x6570;&#x7684;&#x5C5E;&#x6027;"></a>&#x81EA;&#x5B9A;&#x4E49;&#x51FD;&#x6570;&#x7684;&#x5C5E;&#x6027;</h3><p>JavaScript &#x4E2D;&#x7684;&#x51FD;&#x6570;&#x5E76;&#x4E0D;&#x662F;&#x539F;&#x59CB;&#x503C;&#xFF0C;&#x800C;&#x662F;&#x4E00;&#x4E2A;&#x79CD;&#x7279;&#x6B8A;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x62E5;&#x6709;&#x5C5E;&#x6027;&#x3002;&#x5F53;&#x51FD;&#x6570;&#x9700;&#x8981;&#x4E00;&#x4E2A;&#x201D;&#x9759;&#x6001;&#x201D; &#x53D8;&#x91CF;&#x6765;&#x5728;&#x8C03;&#x7528;&#x65F6;&#x4FDD;&#x6301;&#x67D0;&#x4E2A;&#x503C;&#x4E0D;&#x53D8;&#xFF0C;&#x6700;&#x65B9;&#x4FBF;&#x7684;&#x65B9;&#x5F0F;&#x5C31;&#x662F;&#x7ED9;&#x51FD;&#x6570;&#x5B9A;&#x4E49;&#x5C5E;&#x6027;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x5B9A;&#x4E49;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x3002;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">uniquInteger.counter = <span class="number">0</span>;</div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">uniquInteger</span>(<span class="params"></span>) </span>{</div><div class="line">    <span class="keyword">return</span> uniquInteger.counter++; </div><div class="line">}</div></pre></td></tr></table></figure>
<p>&#x8BA1;&#x7B97;&#x9636;&#x4E58;&#x7684;&#x51FD;&#x6570;&#xFF1A;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">factorial</span>(<span class="params">n</span>) </span>{</div><div class="line">    <span class="keyword">if</span> (<span class="built_in">isFinite</span>(n) &amp;&amp; n &gt; <span class="number">0</span> &amp;&amp; n == <span class="built_in">Math</span>.round(n)) { <span class="comment">// &#x6709;&#x9650;&#x7684;&#x6B63;&#x6574;&#x6570;</span></div><div class="line">        <span class="keyword">if</span> (!(n <span class="keyword">in</span> factorial)) </div><div class="line">            factorial[n] = n * factorial(n<span class="number">-1</span>);</div><div class="line">        <span class="keyword">return</span> factorial[n]; </div><div class="line">    }</div><div class="line">    <span class="keyword">else</span> <span class="keyword">return</span> <span class="literal">NaN</span>;</div><div class="line">}</div><div class="line"><span class="built_in">console</span>.log(factorial[<span class="number">1</span>]); <span class="comment">// &#x521D;&#x59CB;&#x5316;</span></div></pre></td></tr></table></figure>
<h2 id="&#x4F5C;&#x4E3A;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x7684;&#x51FD;&#x6570;"><a href="#&#x4F5C;&#x4E3A;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x7684;&#x51FD;&#x6570;" class="headerlink" title="&#x4F5C;&#x4E3A;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x7684;&#x51FD;&#x6570;"></a>&#x4F5C;&#x4E3A;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x7684;&#x51FD;&#x6570;</h2><p>&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x5185;&#x5B9A;&#x4E49;&#x7684;&#x53D8;&#x91CF;&#x4E0D;&#x4F1A;&#x6C61;&#x67D3;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#xFF0C;&#x8FD9;&#x5C31;&#x89E3;&#x51B3;&#x73AF;&#x5883;&#x4E2D;&#x53D8;&#x91CF;&#x51B2;&#x7A81;&#x95EE;&#x9898;&#x3002;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>{</div><div class="line">    <span class="comment">// &#x6A21;&#x5757;&#x4EE3;&#x7801;&#x6BB5;</span></div><div class="line">})();</div></pre></td></tr></table></figure>
<p>&#x4E00;&#x4E2A;&#x4F8B;&#x5B50;&#xFF1A;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// &#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x7528;&#x6765;&#x5C06;&#x7B2C;&#x4E8C;&#x4E2A;&#x4EE5;&#x53CA;&#x540E;&#x7EED;&#x53C2;&#x6570;&#x590D;&#x5236;&#x81F3;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;</span></div><div class="line"><span class="comment">// &#x5982;&#x679C; o &#x7684;&#x5C5E;&#x6027;&#x62E5;&#x6709;&#x4E86;&#x4E00;&#x4E2A;&#x4E0D;&#x53EF;&#x679A;&#x4E3E;&#x7684;&#x540C;&#x540D;&#x5C5E;&#x6027;&#xFF0C;&#x5219; for/in &#x5FAA;&#x73AF;</span></div><div class="line"><span class="comment">// &#x4E0D;&#x4F1A;&#x679A;&#x4E3E;&#x5BF9;&#x8C61; o &#x7684;&#x53EF;&#x679A;&#x4E3E;&#x5C5E;&#x6027;</span></div><div class="line"><span class="keyword">var</span> extend = (<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</div><div class="line">    <span class="comment">// &#x5728;&#x4FEE;&#x590D;&#x4E4B;&#x524D;&#xFF0C;&#x5148;&#x68C0;&#x6D4B; bug &#x662F;&#x5426;&#x5B58;&#x5728;</span></div><div class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> p <span class="keyword">in</span> {<span class="attr">toString</span>: <span class="literal">null</span>}) {</div><div class="line">        <span class="keyword">return</span> <span class="function"><span class="keyword">function</span> <span class="title">extend</span>(<span class="params">o</span>) </span>{</div><div class="line">            <span class="comment">// &#x4EE3;&#x7801;&#x6267;&#x884C;&#x5230;&#x8FD9;&#x91CC;&#xFF0C;for/in &#x5FAA;&#x73AF;&#x4F1A;&#x6B63;&#x786E;&#x5DE5;&#x4F5C;&#x5E76;&#x8FD4;&#x56DE;</span></div><div class="line">            <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">1</span>; i &lt; <span class="built_in">arguments</span>.length; i++) {</div><div class="line">                <span class="keyword">var</span> source = <span class="built_in">arguments</span>[i];</div><div class="line">                <span class="keyword">for</span> (<span class="keyword">var</span> prop <span class="keyword">in</span> source) o[prop] = source[prop];</div><div class="line">            }</div><div class="line">            <span class="keyword">return</span> o;</div><div class="line">        };</div><div class="line">    }</div><div class="line">    <span class="comment">// &#x4EE3;&#x7801;&#x6267;&#x884C;&#x5230;&#x8FD9;&#x91CC;&#xFF0C;&#x8BF4;&#x660E; for/in &#x4E0D;&#x4F1A;&#x679A;&#x4E3E;&#x6D4B;&#x8BD5;&#x5BF9;&#x8C61;&#x7684; toString &#x5C5E;&#x6027;</span></div><div class="line">    <span class="comment">// &#x5982;&#x679C;&#x8FD4;&#x56DE;&#x7684;&#x53E6;&#x4E00;&#x4E2A;&#x7248;&#x672C;&#x7684; extend() &#x51FD;&#x6570;&#xFF0C;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x662F;&#x663E;&#x5F0F;&#x6D4B;&#x8BD5; Object.prototype&#x4E2D;&#x7684;&#x4E0D;&#x53EF;&#x679A;&#x4E3E;&#x5C5E;&#x6027;</span></div><div class="line">    <span class="keyword">return</span> <span class="function"><span class="keyword">function</span> <span class="title">patched_extend</span>(<span class="params">o</span>) </span>{</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">var</span> i =<span class="number">1</span>; i &lt; <span class="built_in">arguments</span>.length; i++) {</div><div class="line">            <span class="keyword">var</span> source = <span class="built_in">arguments</span>[i];</div><div class="line">            <span class="keyword">for</span> (<span class="keyword">var</span> prop <span class="keyword">in</span> source) o[prop] = source[prop];</div><div class="line">            <span class="keyword">for</span> (<span class="keyword">var</span> j = <span class="number">0</span>; j &lt; protoprops.length; i++) {</div><div class="line">                prop = protoprops[j];</div><div class="line">                <span class="keyword">if</span> (source.hasOwnProperty(prop)) o[prop] = source[prop];</div><div class="line">            }</div><div class="line">        }</div><div class="line">        <span class="keyword">return</span> o;</div><div class="line">    };</div><div class="line">    <span class="comment">// &#x5217;&#x51FA;&#x4E86;&#x9700;&#x8981;&#x68C0;&#x6D4B;&#x7684;&#x7279;&#x6B8A;&#x5C5E;&#x6027;</span></div><div class="line">    <span class="keyword">var</span> protoprops = [<span class="string">&quot;toString&quot;</span>, <span class="string">&quot;valueOf&quot;</span>, <span class="string">&quot;constructor&quot;</span>, <span class="string">&quot;hasOwnProperty&quot;</span>,</div><div class="line">        <span class="string">&quot;isPrototypeOf&quot;</span>, <span class="string">&quot;propertyIsEnumerable&quot;</span>, <span class="string">&quot;toLocaleString&quot;</span></div><div class="line">    ];</div><div class="line">}());</div></pre></td></tr></table></figure>
<h2 id="js-&#x8BCD;&#x6CD5;&#x5206;&#x6790;"><a href="#js-&#x8BCD;&#x6CD5;&#x5206;&#x6790;" class="headerlink" title="js &#x8BCD;&#x6CD5;&#x5206;&#x6790;"></a>js &#x8BCD;&#x6CD5;&#x5206;&#x6790;</h2><h3 id="&#x7A0B;&#x5E8F;&#x6267;&#x884C;&#x8FC7;&#x7A0B;"><a href="#&#x7A0B;&#x5E8F;&#x6267;&#x884C;&#x8FC7;&#x7A0B;" class="headerlink" title="&#x7A0B;&#x5E8F;&#x6267;&#x884C;&#x8FC7;&#x7A0B;"></a>&#x7A0B;&#x5E8F;&#x6267;&#x884C;&#x8FC7;&#x7A0B;</h3><ol>
<li>&#x8BFB;&#x53D6;&#x4EE3;&#x7801;&#xFF0C;&#x4E3B;&#x5173;&#x6CE8;&#x58F0;&#x660E;&#x7684;&#x90E8;&#x5206;&#xFF1A;var</li>
<li>&#x5224;&#x65AD;var&#x540E;&#x9762;&#x7684;&#x540D;&#x5B57;&#x662F;&#x5426;&#x5DF2;&#x7ECF;&#x88AB;&#x6807;&#x8BB0;&#xFF0C;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x88AB;&#x6807;&#x8BB0;&#x8FC7;&#xFF0C;&#x5C31;&#x6807;&#x8BB0;</li>
<li>&#x8BFB;&#x53D6;&#x5B8C;&#x6BD5;&#x540E;&#xFF0C;&#x4EE3;&#x7801;&#x4ECE;&#x4E0A;&#x5F80;&#x4E0B;&#xFF0C;&#x4ECE;&#x5DE6;&#x5F80;&#x53F3;&#x4F9D;&#x6B21;&#x6267;&#x884C;</li>
</ol>
<h3 id="&#x8BCD;&#x6CD5;&#x4F5C;&#x7528;&#x57DF;&#xFF08;&#x4F5C;&#x7528;&#x57DF;&#xFF1A;&#x53D8;&#x91CF;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x5230;&#x4E0D;&#x80FD;&#x4F7F;&#x7528;&#x7684;&#x8303;&#x56F4;&#xFF09;"><a href="#&#x8BCD;&#x6CD5;&#x4F5C;&#x7528;&#x57DF;&#xFF08;&#x4F5C;&#x7528;&#x57DF;&#xFF1A;&#x53D8;&#x91CF;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x5230;&#x4E0D;&#x80FD;&#x4F7F;&#x7528;&#x7684;&#x8303;&#x56F4;&#xFF09;" class="headerlink" title="&#x8BCD;&#x6CD5;&#x4F5C;&#x7528;&#x57DF;&#xFF08;&#x4F5C;&#x7528;&#x57DF;&#xFF1A;&#x53D8;&#x91CF;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x5230;&#x4E0D;&#x80FD;&#x4F7F;&#x7528;&#x7684;&#x8303;&#x56F4;&#xFF09;"></a>&#x8BCD;&#x6CD5;&#x4F5C;&#x7528;&#x57DF;&#xFF08;&#x4F5C;&#x7528;&#x57DF;&#xFF1A;&#x53D8;&#x91CF;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x5230;&#x4E0D;&#x80FD;&#x4F7F;&#x7528;&#x7684;&#x8303;&#x56F4;&#xFF09;</h3><blockquote>
<p>&#x8BCD;&#x6CD5;&#x4F5C;&#x7528;&#x57DF;&#x5C31;&#x662F;&#x63CF;&#x8FF0;&#x53D8;&#x91CF;&#x7684;&#x8BBF;&#x95EE;&#x8303;&#x56F4;&#xFF1A;</p>
</blockquote>
<ol>
<li>&#x5728;&#x4EE3;&#x7801;&#x4E2D;&#x53EA;&#x6709;&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x9650;&#x5B9A;&#x4F5C;&#x7528;&#x8303;&#x56F4;&#xFF0C;&#x5141;&#x8BB8;&#x51FD;&#x6570;&#x8BBF;&#x95EE;&#x5916;&#x90E8;&#x7684;&#x53D8;&#x91CF;</li>
<li>&#x5728;&#x51FD;&#x6570;&#x5185;&#x4F18;&#x5148;&#x8BBF;&#x95EE;&#x5185;&#x90E8;&#x58F0;&#x660E;&#x7684;&#x53D8;&#x91CF;&#xFF0C;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x624D;&#x4F1A;&#x8BBF;&#x95EE;&#x5916;&#x90E8;&#x7684;</li>
<li>&#x6240;&#x6709;&#x53D8;&#x91CF;&#x7684;&#x8BBF;&#x95EE;&#x89C4;&#x5219;&#xFF0C;&#x6309;&#x7167;&#x9884;&#x89E3;&#x6790;&#x89C4;&#x5219;&#x6765;&#x8BBF;&#x95EE;</li>
</ol>
<p>&#x4F5C;&#x7528;&#x57DF;&#x94FE;&#xFF1A;</p>
<p>&#x6BCF;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x5177;&#x6709;&#x72EC;&#x7ACB;&#x4F5C;&#x7528;&#x57DF;&#xFF0C;&#x7531;&#x4E8E;&#x51FD;&#x6570;&#x5185;&#x53EF;&#x4EE5;&#x5957;&#x51FD;&#x6570;&#xFF0C;&#x6240;&#x4EE5;&#x5728;&#x51FD;&#x6570;&#x5185;&#x90E8;&#x8BBF;&#x95EE;&#x53D8;&#x91CF;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x9700;&#x8981;&#x4E00;&#x7EA7;&#x4E00;&#x7EA7;&#x7684;&#x5F80;&#x4E0A;&#x67E5;&#x627E;&#x8BE5;&#x53D8;&#x91CF;&#xFF0C;&#x8FD9;&#x6837;&#x5C31;&#x597D;&#x50CF;&#x6784;&#x6210;&#x4E86;&#x4E00;&#x4E2A;&#x94FE;&#x5F0F;&#x7ED3;&#x6784;&#xFF0C;&#x628A;&#x5B83;&#x79F0;&#x4E4B;&#x4E3A;&#x4F5C;&#x7528;&#x57DF;&#x94FE;&#x3002;</p>
<h2 id="&#x4E25;&#x683C;&#x6A21;&#x5F0F;"><a href="#&#x4E25;&#x683C;&#x6A21;&#x5F0F;" class="headerlink" title="&#x4E25;&#x683C;&#x6A21;&#x5F0F;"></a>&#x4E25;&#x683C;&#x6A21;&#x5F0F;</h2><p>&#x5F00;&#x542F;&#x4E25;&#x683C;&#x6A21;&#x5F0F;&#xFF1A;&#x201D;use strict&#x201D;;</p>
<ol>
<li>&#x4E25;&#x683C;&#x6A21;&#x5F0F;&#x4E2D;&#x7981;&#x6B62;&#x7ED9;&#x4E00;&#x4E2A;&#x672A;&#x58F0;&#x660E;&#x7684;&#x53D8;&#x91CF;&#x8D4B;&#x503C;&#xFF1A;</li>
<li>&#x4E25;&#x683C;&#x6A21;&#x5F0F;&#x4E2D;eval&#x5177;&#x6709;&#x4E86;&#x72EC;&#x7ACB;&#x4F5C;&#x7528;&#x57DF;&#x2014;&#x2014;&gt;&#x5728;eval&#x4E2D;&#x58F0;&#x660E;&#x7684;&#x53D8;&#x91CF;&#x548C;&#x51FD;&#x6570;&#x90FD;&#x662F;&#x5C40;&#x90E8;&#x53D8;&#x91CF;</li>
<li>&#x4E25;&#x683C;&#x6A21;&#x5F0F;&#x4E2D;&#x7981;&#x6B62;&#x4F7F;&#x7528;arguments.callee&#x8FDB;&#x884C;&#x9012;&#x5F52;&#x8C03;&#x7528;</li>
</ol>
<h2 id="&#x95ED;&#x5305;"><a href="#&#x95ED;&#x5305;" class="headerlink" title="&#x95ED;&#x5305;"></a>&#x95ED;&#x5305;</h2><p>JavaScript &#x51FD;&#x6570;&#x7684;&#x6267;&#x884C;&#x4F9D;&#x8D56;&#x4E8E;&#x53D8;&#x91CF;&#x4F5C;&#x7528;&#x57DF;&#xFF0C;&#x8FD9;&#x4E2A;&#x4F5C;&#x7528;&#x57DF;&#x5728;&#x51FD;&#x6570;&#x5B9A;&#x4E49;&#x65F6;&#x51B3;&#x5B9A;&#x7684;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x7684;&#x65F6;&#x5019;&#x51B3;&#x5B9A;&#x7684;&#x3002;&#x4E3A;&#x4E86;&#x5B9E;&#x73B0;&#x8FD9;&#x79CD;&#x8BCD;&#x6CD5;&#x4F5C;&#x7528;&#x57DF;, JavaScript &#x51FD;&#x6570;&#x5BF9;&#x8C61;&#x7684;&#x5185;&#x90E8;&#x72B6;&#x6001;&#x4E0D;&#x4EC5;&#x4EC5;&#x5305;&#x542B;&#x51FD;&#x6570;&#x7684;&#x4EE3;&#x7801;&#x903B;&#x8F91;&#xFF0C;&#x8FD8;&#x5FC5;&#x987B;&#x5F15;&#x7528;&#x5F53;&#x524D;&#x7684;&#x4F5C;&#x7528;&#x57DF;&#xFF0C; JavaScript &#x51FD;&#x6570;&#x5BF9;&#x8C61;&#x7684;&#x5185;&#x90E8;&#x72B6;&#x6001;&#x4E0D;&#x4EC5;&#x5305;&#x542B;&#x51FD;&#x6570;&#x4EE3;&#x7801;&#x903B;&#x8F91;&#xFF0C;&#x8FD8;&#x5FC5;&#x987B;&#x5F15;&#x7528;&#x5F53;&#x524D;&#x7684;&#x4F5C;&#x7528;&#x57DF;&#x94FE;&#x3002;&#x51FD;&#x6570;&#x5BF9;&#x8C61;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x4F5C;&#x7528;&#x57DF;&#x94FE;&#x76F8;&#x4E92;&#x5173;&#x8054;&#x8D77;&#x6765;&#xFF0C;&#x51FD;&#x6570;&#x4F53;&#x5185;&#x90E8;&#x7684;&#x53D8;&#x91CF;&#x90FD;&#x53EF;&#x4EE5;&#x4FDD;&#x5B58;&#x5728;&#x51FD;&#x6570;&#x4F5C;&#x7528;&#x57DF;&#x5185;&#xFF0C;&#x8FD9;&#x79CD;&#x7279;&#x6027;&#x79F0;&#x95ED;&#x5305;&#x3002;</p>
<p>&#x51FD;&#x6570;&#x5B9A;&#x4E49;&#x65F6;&#x7684;&#x5B9A;&#x4E49;&#x4F5C;&#x7528;&#x57DF;&#x94FE;&#x5230;&#x51FD;&#x6570;&#x6267;&#x884C;&#x65F6;&#x4F9D;&#x7136;&#x6709;&#x6548;&#x3002;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// &#x5229;&#x7528;&#x95ED;&#x5305;&#x5B9E;&#x73B0;&#x7684;&#x79C1;&#x6709;&#x5C5E;&#x6027;&#x5B58;&#x53D6;&#x5668;&#x65B9;&#x6CD5;</span></div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">addPrivateProperty</span> (<span class="params">o, name, predicate</span>) </span>{</div><div class="line">    <span class="keyword">var</span> value;</div><div class="line">    o[<span class="string">&quot;get&quot;</span> + name] = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>{ <span class="keyword">return</span> value; };</div><div class="line">    o[<span class="string">&quot;set&quot;</span> + name] = <span class="function"><span class="keyword">function</span>(<span class="params">v</span>) </span>{</div><div class="line">        <span class="keyword">if</span> (predicate &amp;&amp; !predicate(v))</div><div class="line">            <span class="keyword">throw</span> <span class="built_in">Error</span>(<span class="string">&quot;set&quot;</span> + name + <span class="string">&quot;: invalid value &quot;</span> + v);</div><div class="line">        <span class="keyword">else</span> </div><div class="line">            value = v;</div><div class="line">    };</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">var</span> o = {};</div><div class="line">addPrivateProperty(o, <span class="string">&quot;Name&quot;</span>, <span class="function"><span class="keyword">function</span>(<span class="params">x</span>) </span>{ <span class="keyword">return</span> <span class="keyword">typeof</span> x == <span class="string">&quot;string&quot;</span>});</div><div class="line">o.setName(<span class="string">&quot;Hiraku&quot;</span>);  <span class="comment">// &#x8BBE;&#x7F6E;&#x5C5E;&#x6027;&#x503C;</span></div><div class="line"><span class="built_in">console</span>.log(o.getName); <span class="comment">// &#x83B7;&#x53D6;&#x5C5E;&#x6027;&#x503C;</span></div><div class="line">o.setName(<span class="number">0</span>);  <span class="comment">// &#x8BD5;&#x56FE;&#x8BBE;&#x7F6E;&#x4E00;&#x4E2A;&#x9519;&#x8BEF;&#x7684;&#x503C;</span></div></pre></td></tr></table></figure>
<p>&#x95ED;&#x5305;&#x7684;&#x4E00;&#x4E2A;&#x5E94;&#x7528;: &#x6A21;&#x5757;&#x5316;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//&#x9762;&#x5411;&#x5BF9;&#x8C61;&#x2014;&#x2014;&gt;&#x6A21;&#x5757;&#x5316;</span></div><div class="line"><span class="keyword">var</span> SongManager2=(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</div><div class="line">    <span class="function"><span class="keyword">function</span> <span class="title">f1</span>(<span class="params"></span>)</span>{}</div><div class="line">    <span class="function"><span class="keyword">function</span> <span class="title">f2</span>(<span class="params"></span>)</span>{}</div><div class="line">    <span class="function"><span class="keyword">function</span> <span class="title">f3</span>(<span class="params"></span>)</span>{}</div><div class="line">    <span class="comment">//&#x6784;&#x9020;&#x51FD;&#x6570;</span></div><div class="line">    <span class="function"><span class="keyword">function</span> <span class="title">SongManager</span>(<span class="params"></span>)</span>{}</div><div class="line">    SongManager.prototype={ <span class="comment">//&#x539F;&#x578B;&#x5BF9;&#x8C61;</span></div><div class="line">        init:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</div><div class="line">            f1();</div><div class="line">            f2();</div><div class="line">            f3();<span class="comment">//&#x8FD9;3&#x4E2A;&#x529F;&#x80FD;&#xFF1A;&#x5982;&#x679C;&#x4E1A;&#x52A1;&#x903B;&#x8F91;&#x975E;&#x5E38;&#x590D;&#x6742;&#xFF0C;&#x9700;&#x8981;&#x628A;&#x8FD9;&#x4E9B;&#x65B9;&#x6CD5;&#x62C6;&#x5206;&#x6389;&#xFF0C;</span></div><div class="line">            <span class="comment">//     &#x5E76;&#x4E14;&#x8FD9;&#x4E9B;&#x65B9;&#x6CD5;&#x4E0D;&#x80FD;&#x8BA9;&#x7528;&#x6237;&#x968F;&#x4FBF;&#x8C03;&#x7528;&#xFF0C;&#x7528;&#x4E00;&#x4E9B;&#x51FD;&#x6570;&#x5C01;&#x88C5;&#x4E00;&#x4E0B;</span></div><div class="line">        },</div><div class="line">        <span class="comment">//&#x539F;&#x578B;&#x5BF9;&#x8C61;&#x4E2D;&#x7684;&#x65B9;&#x6CD5;&#x5BF9;&#x4E8E;&#x5B50;&#x5BF9;&#x8C61;&#x662F;&#x5B8C;&#x5168;&#x516C;&#x5F00;&#x7684;&#xFF0C;&#x5BF9;&#x8C61;&#x53EF;&#x4EE5;&#x968F;&#x610F;&#x8C03;&#x7528;</span></div><div class="line">        init1:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{},</div><div class="line">        <span class="attr">init2</span>:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{}</div><div class="line">    };</div><div class="line">    <span class="keyword">return</span> SongManager;</div><div class="line">}());</div></pre></td></tr></table></figure>
<h3 id="&#x95ED;&#x5305;&#x5B9E;&#x73B0;&#x601D;&#x8DEF;&#xFF1A;&#x5916;&#x5C42;&#x51FD;&#x6570;&#xFF0C;&#x5185;&#x5C42;&#x51FD;&#x6570;"><a href="#&#x95ED;&#x5305;&#x5B9E;&#x73B0;&#x601D;&#x8DEF;&#xFF1A;&#x5916;&#x5C42;&#x51FD;&#x6570;&#xFF0C;&#x5185;&#x5C42;&#x51FD;&#x6570;" class="headerlink" title="&#x95ED;&#x5305;&#x5B9E;&#x73B0;&#x601D;&#x8DEF;&#xFF1A;&#x5916;&#x5C42;&#x51FD;&#x6570;&#xFF0C;&#x5185;&#x5C42;&#x51FD;&#x6570;"></a>&#x95ED;&#x5305;&#x5B9E;&#x73B0;&#x601D;&#x8DEF;&#xFF1A;&#x5916;&#x5C42;&#x51FD;&#x6570;&#xFF0C;&#x5185;&#x5C42;&#x51FD;&#x6570;</h3><ul>
<li>&#x901A;&#x5E38;&#x8BBE;&#x7F6E;&#x5916;&#x5C42;&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x5C31;&#x662F;&#x5185;&#x5C42;&#x51FD;&#x6570;</li>
<li>&#x4E5F;&#x53EF;&#x4EE5;&#x8BA9;&#x5916;&#x5C42;&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x662F;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#xFF08;&#x65B9;&#x6CD5;)</li>
<li>&#x5982;&#x679C;&#x9700;&#x8981;&#x4FDD;&#x5B58;&#x4E00;&#x4E2A;&#x6570;&#x636E;(&#x5916;&#x5C42;&#x51FD;&#x6570;&#x7684;&#x540C;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;)&#xFF0C;&#x8BA9;&#x5185;&#x5C42;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x591A;&#x6B21;&#xFF0C;&#x8BE5;&#x53D8;&#x91CF;&#x7684;&#x503C;&#x90FD;&#x662F;&#x5171;&#x4EAB;&#x7684;</li>
<li>&#x5982;&#x679C;&#x9700;&#x8981;&#x4FDD;&#x5B58;&#x591A;&#x4E2A;&#x6570;&#x636E;(&#x5916;&#x5C42;&#x51FD;&#x6570;&#x7684;&#x540C;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;)&#xFF0C;&#x8BA9;&#x5916;&#x5C42;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x591A;&#x6B21;</li>
</ul>
<h2 id="&#x51FD;&#x6570;&#x7684;&#x5C5E;&#x6027;&#x3001;&#x65B9;&#x6CD5;&#x548C;&#x6784;&#x9020;&#x51FD;&#x6570;"><a href="#&#x51FD;&#x6570;&#x7684;&#x5C5E;&#x6027;&#x3001;&#x65B9;&#x6CD5;&#x548C;&#x6784;&#x9020;&#x51FD;&#x6570;" class="headerlink" title="&#x51FD;&#x6570;&#x7684;&#x5C5E;&#x6027;&#x3001;&#x65B9;&#x6CD5;&#x548C;&#x6784;&#x9020;&#x51FD;&#x6570;"></a>&#x51FD;&#x6570;&#x7684;&#x5C5E;&#x6027;&#x3001;&#x65B9;&#x6CD5;&#x548C;&#x6784;&#x9020;&#x51FD;&#x6570;</h2><p>JavaScript &#x4E2D;&#x7684;&#x51FD;&#x6570;&#x662F;&#x503C;&#xFF0C; &#x4F7F;&#x7528; typeof &#x65B9;&#x6CD5;&#x5F97;&#x5230;&#x7684;&#x7ED3;&#x679C;&#x662F; &#x201C;function&#x201D;&#xFF0C;&#x4F46;&#x662F;&#x51FD;&#x6570;&#x662F; JavaScript &#x4E2D;&#x7684;&#x7279;&#x6B8A;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x62E5;&#x6709;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;&#x3002;&#x751A;&#x81F3;&#x53EF;&#x4EE5;&#x7528; Function()&#x6784;&#x9020;&#x51FD;&#x6570;&#x6765;&#x521B;&#x5EFA;&#x65B0;&#x7684;&#x51FD;&#x6570;&#x5BF9;&#x8C61;&#x3002;</p>
<h3 id="length-&#x5C5E;&#x6027;"><a href="#length-&#x5C5E;&#x6027;" class="headerlink" title="length &#x5C5E;&#x6027;"></a>length &#x5C5E;&#x6027;</h3><p>&#x5728;&#x51FD;&#x6570;&#x4F53;&#x91CC;&#xFF0C; arguments.length &#x8868;&#x793A;&#x4F20;&#x5165;&#x51FD;&#x6570;&#x7684;&#x5B9E;&#x53C2;&#x7684;&#x4E2A;&#x6570;&#x3002;&#x800C;&#x51FD;&#x6570;&#x672C;&#x4E8B;&#x7684; length &#x5C5E;&#x6027;&#x662F;&#x53EA;&#x8BFB;&#x7684;&#xFF0C;&#x5B83;&#x4EE3;&#x8868;&#x51FD;&#x6570;&#x5F62;&#x53C2;&#x6570;&#x91CF;&#xFF0C;&#x5373;&#x51FD;&#x6570;&#x5B9A;&#x4E49;&#x65F6;&#x65F6;&#x7ED9;&#x51FA;&#x7684;&#x5B9E;&#x53C2;&#x4E2A;&#x6570;&#xFF0C;&#x901A;&#x5E38;&#x4E5F;&#x662F;&#x51FD;&#x6570;&#x5728;&#x8C03;&#x7528;&#x65F6;&#x671F;&#x671B;&#x4F20;&#x5165;&#x7684;&#x4E2A;&#x6570;&#x3002;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// &#x5224;&#x65AD;&#x6240;&#x4F20;&#x5165;&#x7684;&#x5B9E;&#x53C2;&#x4E2A;&#x6570;&#x662F;&#x5426;&#x6B63;&#x786E;</span></div><div class="line"><span class="comment">// &#x8BE5;&#x51FD;&#x6570;&#x4F7F;&#x7528; arguments.callee &#x4E0D;&#x80FD;&#x5728;&#x4E25;&#x683C;&#x6A21;&#x5F0F;&#x4E0B;&#x8C03;&#x7528;</span></div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">check</span>(<span class="params">args</span>) </span>{</div><div class="line">    <span class="keyword">var</span> actual = args.length;</div><div class="line">    <span class="keyword">var</span> expected = args.callee.length;</div><div class="line">    <span class="keyword">if</span> (actural !== expected) </div><div class="line">        <span class="keyword">throw</span> <span class="built_in">Error</span>(<span class="string">&quot;Expected &quot;</span> + expected + <span class="string">&quot;args; got &quot;</span> + actual);</div><div class="line">}</div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">f</span>(<span class="params">x, y, z</span>) </span>{</div><div class="line">    check(<span class="built_in">arguments</span>);</div><div class="line">    <span class="keyword">return</span> x + y + z;</div><div class="line">}</div></pre></td></tr></table></figure>
<h3 id="prototype-&#x5C5E;&#x6027;"><a href="#prototype-&#x5C5E;&#x6027;" class="headerlink" title="prototype &#x5C5E;&#x6027;"></a>prototype &#x5C5E;&#x6027;</h3><p>&#x6BCF;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x90FD;&#x5305;&#x542B;&#x4E00;&#x4E2A; prototype &#x5C5E;&#x6027;&#xFF0C;&#x8BE5;&#x5C5E;&#x6027;&#x6307;&#x5411;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;&#x5F15;&#x7528;&#xFF0C;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x79F0;&#x4E3A;&#x539F;&#x578B;&#x5BF9;&#x8C61;&#x3002;&#x4E0B;&#x4E00;&#x7BC7;&#x6587;&#x7AE0;&#x5C06;&#x6DF1;&#x5165;&#x5206;&#x6790;&#x3002;</p>
<h3 id="call-&#x65B9;&#x6CD5;&#x548C;-apply-&#x65B9;&#x6CD5;"><a href="#call-&#x65B9;&#x6CD5;&#x548C;-apply-&#x65B9;&#x6CD5;" class="headerlink" title="call() &#x65B9;&#x6CD5;&#x548C; apply() &#x65B9;&#x6CD5;"></a>call() &#x65B9;&#x6CD5;&#x548C; apply() &#x65B9;&#x6CD5;</h3><p>call() &#x548C; apply() &#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x5B9E;&#x53C2;&#x662F;&#x8981;&#x8C03;&#x7528;&#x51FD;&#x6570;&#x7684;&#x6BCD;&#x4F53;&#x5BF9;&#x8C61;&#xFF0C;&#x5B83;&#x662F;&#x8C03;&#x7528;&#x4E0A;&#x4E0B;&#x6587;&#xFF0C;&#x5728;&#x51FD;&#x6570;&#x4F53;&#x5185;&#x901A;&#x8FC7; this &#x6765;&#x83B7;&#x5F97;&#x5BF9;&#x5B83;&#x7684;&#x5F15;&#x7528;&#x3002;</p>
<p>&#x5728;&#x4E25;&#x683C;&#x6A21;&#x5F0F;&#x4E2D;&#xFF0C;call() &#x548C; apply() &#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x5B9E;&#x53C2;&#x90FD;&#x4F1A;&#x53D8;&#x6210; this &#x7684;&#x503C;&#xFF0C;&#x54EA;&#x6015;&#x4F20;&#x5165;&#x7684;&#x662F; null &#x6216; undefined&#x3002;&#x5728;&#x4E25;&#x683C;&#x6A21;&#x5F0F;&#x4E0B;&#xFF0C;&#x4F20;&#x5165; null &#x6216; undefined &#x7684;&#x65F6;&#x5019;&#x90FD;&#x4F1A;&#x88AB;&#x5168;&#x5C40;&#x5BF9;&#x8C61;&#x4EE3;&#x66FF;&#x3002;</p>
<h3 id="call-apply&#x533A;&#x522B;"><a href="#call-apply&#x533A;&#x522B;" class="headerlink" title="call/apply&#x533A;&#x522B;"></a>call/apply&#x533A;&#x522B;</h3><p>1.&#x76F8;&#x540C;&#x70B9;&#xFF1A;</p>
<ul>
<li>(1) &#x90FD;&#x662F;Function.prototype&#x5BF9;&#x8C61;&#x4E2D;&#x5B9A;&#x4E49;&#x7684;&#x65B9;&#x6CD5;</li>
<li>(2) &#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x90FD;&#x662F;&#x8868;&#x793A;&#x51FD;&#x6570;&#x5185;&#x90E8;&#x7684;this&#x7684;&#x503C;</li>
</ul>
<ol>
<li>&#x4E0D;&#x540C;&#x70B9;&#xFF1A;</li>
</ol>
<ul>
<li>&#x5982;&#x679C;&#x9700;&#x8981;&#x7ED9;&#x51FD;&#x6570;&#x4F20;&#x9012;&#x53C2;&#x6570;&#x7684;&#x65F6;&#x5019;:<ul>
<li>&#x5229;&#x7528;call&#x65B9;&#x6CD5;&#xFF0C;&#x5C06;&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;&#x4ECE;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x5F00;&#x59CB;&#x4F9D;&#x6B21;&#x6392;&#x5F00;</li>
<li>apply&#x65B9;&#x6CD5;&#x7684;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x662F;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;&#x5BF9;&#x8C61;&#xFF0C;&#x6570;&#x7EC4;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x8868;&#x793A;&#x51FD;&#x6570;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x5B9E;&#x53C2;&#xFF0C;&#x4F9D;&#x6B21;&#x4EE5;&#x6B64;&#x7C7B;&#x63A8;</li>
</ul>
</li>
</ul>
<h3 id="apply&#x7684;&#x4E00;&#x4E2A;&#x6F02;&#x4EAE;&#x7684;&#x5E94;&#x7528;"><a href="#apply&#x7684;&#x4E00;&#x4E2A;&#x6F02;&#x4EAE;&#x7684;&#x5E94;&#x7528;" class="headerlink" title="apply&#x7684;&#x4E00;&#x4E2A;&#x6F02;&#x4EAE;&#x7684;&#x5E94;&#x7528;"></a>apply&#x7684;&#x4E00;&#x4E2A;&#x6F02;&#x4EAE;&#x7684;&#x5E94;&#x7528;</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"> <span class="keyword">var</span> points = [</div><div class="line">        { <span class="attr">x</span>: <span class="number">110</span>, <span class="attr">y</span>: <span class="number">50</span>}, { <span class="attr">x</span>: <span class="number">130</span>, <span class="attr">y</span>: <span class="number">60</span> }, { <span class="attr">x</span>: <span class="number">20</span>, <span class="attr">y</span>: <span class="number">70</span> }, { <span class="attr">x</span>: <span class="number">60</span>, <span class="attr">y</span>: <span class="number">50</span> }</div><div class="line">    ];</div><div class="line"><span class="keyword">var</span> maxX = <span class="built_in">Math</span>.max.apply( <span class="literal">null</span>, points.map(<span class="function"><span class="keyword">function</span> (<span class="params">v</span>) </span>{ <span class="keyword">return</span> v.x; }));</div></pre></td></tr></table></figure>
<p>&#x4EE5;&#x4E0A;&#x4EE3;&#x7801;&#x4E2D;&#x501F;&#x7528;Math&#x5BF9;&#x7684;max&#x65B9;&#x6CD5;&#xFF0C;&#x5229;&#x7528;arr.map()&#x65B9;&#x6CD5;&#x4E2D;&#x8FD4;&#x56DE;&#x7684;&#x662F;&#x6570;&#x7EC4;&#x8FD9;&#x4E00;&#x7279;&#x6027;&#x5F97;&#x5230;&#x4E86;&#x6570;&#x7EC4;&#x4E2D;&#x5BF9;&#x8C61;&#x7684;&#x67D0;&#x4E2A;&#x5C5E;&#x6027;&#x7684;&#x6700;&#x5927;&#x503C;&#x3002;</p>
<h3 id="&#x5C06;&#x5F53;&#x524D;&#x51FD;&#x6570;&#x7684;-arguments-&#x6570;&#x7EC4;&#x76F4;&#x63A5;&#x4F20;&#x5165;-apply-&#x6765;&#x8C03;&#x7528;&#x53E6;&#x4E00;&#x4E2A;&#x51FD;&#x6570;"><a href="#&#x5C06;&#x5F53;&#x524D;&#x51FD;&#x6570;&#x7684;-arguments-&#x6570;&#x7EC4;&#x76F4;&#x63A5;&#x4F20;&#x5165;-apply-&#x6765;&#x8C03;&#x7528;&#x53E6;&#x4E00;&#x4E2A;&#x51FD;&#x6570;" class="headerlink" title="&#x5C06;&#x5F53;&#x524D;&#x51FD;&#x6570;&#x7684; arguments &#x6570;&#x7EC4;&#x76F4;&#x63A5;&#x4F20;&#x5165; apply() &#x6765;&#x8C03;&#x7528;&#x53E6;&#x4E00;&#x4E2A;&#x51FD;&#x6570;"></a>&#x5C06;&#x5F53;&#x524D;&#x51FD;&#x6570;&#x7684; arguments &#x6570;&#x7EC4;&#x76F4;&#x63A5;&#x4F20;&#x5165; apply() &#x6765;&#x8C03;&#x7528;&#x53E6;&#x4E00;&#x4E2A;&#x51FD;&#x6570;</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// &#x5C06;&#x5BF9;&#x8C61; o &#x4E2D;&#x7684;&#x65B9;&#x6CD5;&#x66FF;&#x6362;&#x4E3A;&#x53E6;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;</span></div><div class="line"><span class="comment">// &#x53EF;&#x4EE5;&#x5728;&#x8C03;&#x7528;&#x539F;&#x59CB;&#x65B9;&#x6CD5;&#x4E4B;&#x524D;&#x548C;&#x4E4B;&#x540E;&#x8BB0;&#x5F55;&#x65E5;&#x5FD7;&#x6D88;&#x606F;</span></div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">trace</span>(<span class="params">o, m</span>) </span>{</div><div class="line">    <span class="keyword">var</span> original = o[m]; <span class="comment">// &#x5728;&#x95ED;&#x5305;&#x4E2D;&#x4FDD;&#x5B58;&#x539F;&#x6709;&#x65B9;&#x6CD5;</span></div><div class="line">    o[m] = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{ <span class="comment">// &#x5B9A;&#x4E49;&#x65B0;&#x65B9;&#x6CD5;</span></div><div class="line">      <span class="built_in">console</span>.log(<span class="keyword">new</span> <span class="built_in">Date</span>(), <span class="string">&quot;Entering:&quot;</span>, m); <span class="comment">// &#x8F93;&#x51FA;&#x65E5;&#x5FD7;&#x6D88;&#x606F;</span></div><div class="line">      <span class="keyword">var</span> result = original.apply(<span class="keyword">this</span>, <span class="built_in">arguments</span>); <span class="comment">// &#x8C03;&#x7528;&#x539F;&#x59CB;&#x51FD;&#x6570;</span></div><div class="line">      <span class="built_in">console</span>.log(<span class="keyword">new</span> <span class="built_in">Date</span>(), <span class="string">&quot;Exiting:&quot;</span>, m); <span class="comment">// &#x8F93;&#x51FA;&#x65E5;&#x5FD7;&#x6D88;&#x606F;</span></div><div class="line">      <span class="keyword">return</span> result; <span class="comment">// &#x8FD4;&#x56DE;&#x7ED3;&#x679C;</span></div><div class="line">    };</div><div class="line">}</div></pre></td></tr></table></figure>
<p>trace &#x65B9;&#x6CD5;&#x63A5;&#x6536;&#x4E24;&#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x548C;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;&#x540D;&#xFF0C;&#x5B83;&#x5C06;&#x5236;&#x5B9A;&#x7684;&#x65B9;&#x6CD5;&#x66FF;&#x6362;&#x4E3A;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x65B9;&#x6CD5;&#x3002;</p>
<h3 id="bind-&#x65B9;&#x6CD5;"><a href="#bind-&#x65B9;&#x6CD5;" class="headerlink" title="bind() &#x65B9;&#x6CD5;"></a>bind() &#x65B9;&#x6CD5;</h3><p>bind() &#x65B9;&#x6CD5;&#x5728; Function &#x7684;&#x539F;&#x578B;&#x5BF9;&#x8C61;&#x4E0A;</p>
<p>bind &#x65B9;&#x6CD5;&#x662F; ECMAScript 5 &#x7684;&#x65B0;&#x65B9;&#x6CD5;&#xFF0C;&#x7528;&#x9014;&#x662F;&#x5C06;&#x51FD;&#x6570;&#x7ED1;&#x5B9A;&#x5230;&#x67D0;&#x4E2A;&#x5BF9;&#x8C61;&#x3002;&#x5F53;&#x51FD;&#x6570; f() &#x4E0A;&#x8C03;&#x7528; bind &#x65B9;&#x6CD5;&#x5E76;&#x4F20;&#x5165;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61; o &#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#xFF0C;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x5BF9;&#x8C61;&#x3002;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">f</span>(<span class="params">y</span>) </span>{ <span class="keyword">return</span> <span class="keyword">this</span>.x + y; }; </div><div class="line"><span class="keyword">var</span> o = {<span class="attr">x</span>: <span class="number">1</span>};</div><div class="line"><span class="keyword">var</span> g = f.bind(o);</div><div class="line"><span class="built_in">console</span>.log(g(<span class="number">2</span>));  <span class="comment">// 3</span></div></pre></td></tr></table></figure>
<p>bind() &#x7B80;&#x5355;&#x7ED1;&#x5B9A;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// &#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x901A;&#x8FC7;&#x8C03;&#x7528;&#x5B83;&#x6765;&#x8C03;&#x7528; o &#x4E2D;&#x7684;&#x65B9;&#x6CD5; f(), &#x4F20;&#x9012;&#x5B83;&#x6240;&#x6709;&#x7684;&#x5B9E;&#x53C2;</span></div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">bind</span>(<span class="params">f, o</span>) </span>{</div><div class="line">    <span class="keyword">if</span> (f.bind) <span class="keyword">return</span> f.bind(o);</div><div class="line">    <span class="keyword">else</span> <span class="keyword">return</span> <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>{</div><div class="line">        <span class="keyword">return</span> f.apply(o, <span class="built_in">arguments</span>);</div><div class="line">    }</div><div class="line">}</div></pre></td></tr></table></figure>
<p>ECMAScript 5 &#x7684; bind() &#x65B9;&#x6CD5;&#x4E0D;&#x4EC5;&#x4EC5;&#x662F;&#x5C06;&#x51FD;&#x6570;&#x7ED1;&#x5B9A;&#x5230;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#xFF0C;&#x5B83;&#x8FD8;&#x53EF;&#x4EE5;&#x9644;&#x5E26;&#x4E00;&#x4E9B;&#x5176;&#x5B83;&#x5E94;&#x7528;&#xFF1A;&#x9664;&#x4E86;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x5916;&#xFF0C;&#x4F20;&#x5165; bind() &#x7684;&#x5B9E;&#x53C2;&#x4E5F;&#x4F1A;&#x7ED1;&#x5B9A;&#x5230; this&#x3002;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> sum = <span class="function"><span class="keyword">function</span>(<span class="params">x, y</span>) </span>{ <span class="keyword">return</span> x + y; };</div><div class="line"><span class="comment">// &#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x7C7B;&#x4F3C; sum &#x7684;&#x51FD;&#x6570;&#xFF0C;&#x4F46; this &#x7684;&#x503C;&#x7ED1;&#x5B9A;&#x5230; null</span></div><div class="line"><span class="comment">// &#x5E76;&#x4E14;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x7ED1;&#x5B9A;&#x5230; 1&#xFF0C;&#x8FD9;&#x4E2A;&#x65B0;&#x7684;&#x51FD;&#x6570;&#x671F;&#x671B;&#x53EA;&#x4F20;&#x5165;&#x4E00;&#x4E2A;&#x5B9E;&#x53C2;</span></div><div class="line"><span class="keyword">var</span> succ = sum.bind(<span class="literal">null</span>, <span class="number">1</span>);</div><div class="line"><span class="built_in">console</span>.log(succ(<span class="number">2</span>));  <span class="comment">// 3: x &#x7ED1;&#x5B9A;&#x5230; 1&#xFF0C; &#x5E76;&#x4F20;&#x5165; 2 &#x4F5C;&#x4E3A;&#x5B9E;&#x53C2; y</span></div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">f</span>(<span class="params">y,z</span>) </span>{ <span class="keyword">return</span> <span class="keyword">this</span>.x + y + z };</div><div class="line"><span class="keyword">var</span> g = f.bind({<span class="attr">x</span>:<span class="number">1</span>}, <span class="number">2</span>);</div><div class="line"><span class="built_in">console</span>.log(g(<span class="number">3</span>));  <span class="comment">// 6: this.x &#x7ED1;&#x5B9A;&#x5230; 1&#xFF0C;y &#x7ED1;&#x5B9A;&#x5230; 2&#xFF0C; z &#x7ED1;&#x5B9A;&#x5230; 3</span></div></pre></td></tr></table></figure>
<h3 id="bind-&#x7684;&#x517C;&#x5BB9;&#x65B9;&#x6CD5;"><a href="#bind-&#x7684;&#x517C;&#x5BB9;&#x65B9;&#x6CD5;" class="headerlink" title="bind() &#x7684;&#x517C;&#x5BB9;&#x65B9;&#x6CD5;"></a>bind() &#x7684;&#x517C;&#x5BB9;&#x65B9;&#x6CD5;</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">if</span> (!<span class="built_in">Function</span>.prototype.bind) {</div><div class="line">    <span class="built_in">Function</span>.prototype.bind = <span class="function"><span class="keyword">function</span> (<span class="params">o <span class="regexp">/*, args*/</span></span>) </span>{</div><div class="line">        <span class="comment">// &#x5C06; this &#x548C; arguments &#x7684;&#x503C;&#x4FDD;&#x5B58;&#x5230;&#x53D8;&#x91CF;&#x4E2D;</span></div><div class="line">        <span class="keyword">var</span> self = <span class="keyword">this</span>, boundArgs = <span class="built_in">arguments</span>;</div><div class="line">        <span class="keyword">return</span> <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>{</div><div class="line">            <span class="keyword">var</span> args = [], i;</div><div class="line">            <span class="keyword">for</span> (i = <span class="number">1</span>; i &lt; boundArgs.length; i++) args.push(boundArgs[i]);</div><div class="line">            <span class="keyword">for</span> (i = <span class="number">1</span>; i &lt; <span class="built_in">arguments</span>.length; i++) args.push(<span class="built_in">arguments</span>[i]);</div><div class="line">            <span class="keyword">return</span> self.apply(o, args);</div><div class="line">        };</div><div class="line">    };</div><div class="line">}</div></pre></td></tr></table></figure>
<p>ECMAScript 5 &#x7684; bind() &#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x7684;&#x51FD;&#x6570;&#x4E0D;&#x5305;&#x542B; prototype &#x5C5E;&#x6027;&#xFF0C;&#x5E76;&#x4E14;&#x5C06;&#x8FD9;&#x4E9B;&#x7ED1;&#x5B9A;&#x7684;&#x51FD;&#x6570;&#x7528;&#x4F5C;&#x6784;&#x9020;&#x51FD;&#x6570;&#x65F6;&#x6240;&#x521B;&#x5EFA;&#x7684;&#x5BF9;&#x8C61;&#x4ECE;&#x539F;&#x59CB;&#x7684;&#x672A;&#x7ED1;&#x5B9A;&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#x4E2D;&#x7EE7;&#x627F; prototype &#x3002;</p>
<h3 id="toString-&#x65B9;&#x6CD5;"><a href="#toString-&#x65B9;&#x6CD5;" class="headerlink" title="toString() &#x65B9;&#x6CD5;"></a>toString() &#x65B9;&#x6CD5;</h3><p>&#x51FD;&#x6570;&#x4E5F;&#x6709; toString() &#x65B9;&#x6CD5;&#xFF0C;&#x5927;&#x591A;&#x6570;&#x51FD;&#x6570;&#x7684; toString() &#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x5305;&#x542B;&#x51FD;&#x6570;&#x4F53;&#x672C;&#x8EAB;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x3002;</p>
<h3 id="&#x9759;&#x6001;&#x5C5E;&#x6027;&#x548C;&#x5B9E;&#x4F8B;&#x5C5E;&#x6027;"><a href="#&#x9759;&#x6001;&#x5C5E;&#x6027;&#x548C;&#x5B9E;&#x4F8B;&#x5C5E;&#x6027;" class="headerlink" title="&#x9759;&#x6001;&#x5C5E;&#x6027;&#x548C;&#x5B9E;&#x4F8B;&#x5C5E;&#x6027;"></a>&#x9759;&#x6001;&#x5C5E;&#x6027;&#x548C;&#x5B9E;&#x4F8B;&#x5C5E;&#x6027;</h3><ol>
<li>&#x7ED9;&#x51FD;&#x6570;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x5C5E;&#x6027;&#xFF08;&#x9759;&#x6001;&#x5C5E;&#x6027;&#x2014;&#x2014;&gt;&#x51FD;&#x6570;&#x5BF9;&#x8C61;&#x81EA;&#x8EAB;&#x7684;&#x5C5E;&#x6027;&#xFF09;</li>
<li>&#x7ED9;&#x67D0;&#x4E2A;&#x6784;&#x9020;&#x51FD;&#x6570;&#x7684;&#x5B9E;&#x4F8B;&#x6DFB;&#x52A0;&#x7684;&#x5C5E;&#x6027;&#xFF1A;&#x5B9E;&#x4F8B;&#x5C5E;&#x6027;</li>
</ol>
<h3 id="&#x6240;&#x6709;&#x7684;&#x51FD;&#x6570;&#x5BF9;&#x8C61;&#x90FD;&#x5171;&#x6709;&#x7684;&#x4E00;&#x4E9B;&#x9759;&#x6001;&#x5C5E;&#x6027;"><a href="#&#x6240;&#x6709;&#x7684;&#x51FD;&#x6570;&#x5BF9;&#x8C61;&#x90FD;&#x5171;&#x6709;&#x7684;&#x4E00;&#x4E9B;&#x9759;&#x6001;&#x5C5E;&#x6027;" class="headerlink" title="&#x6240;&#x6709;&#x7684;&#x51FD;&#x6570;&#x5BF9;&#x8C61;&#x90FD;&#x5171;&#x6709;&#x7684;&#x4E00;&#x4E9B;&#x9759;&#x6001;&#x5C5E;&#x6027;"></a>&#x6240;&#x6709;&#x7684;&#x51FD;&#x6570;&#x5BF9;&#x8C61;&#x90FD;&#x5171;&#x6709;&#x7684;&#x4E00;&#x4E9B;&#x9759;&#x6001;&#x5C5E;&#x6027;</h3><ol>
<li>name&#xFF1A;&#x83B7;&#x53D6;&#x51FD;&#x6570;&#x7684;&#x540D;&#x79F0;</li>
<li>length&#xFF1A;&#x8868;&#x793A;&#x51FD;&#x6570;&#x5F62;&#x53C2;&#x7684;&#x4E2A;&#x6570;</li>
<li>caller&#xFF1A;&#x8868;&#x793A;&#x5F53;&#x524D;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x662F;&#x5728;&#x54EA;&#x4E2A;&#x51FD;&#x6570;&#x5185;</li>
</ol>
<h3 id="Function-&#x6784;&#x9020;&#x51FD;&#x6570;"><a href="#Function-&#x6784;&#x9020;&#x51FD;&#x6570;" class="headerlink" title="Function() &#x6784;&#x9020;&#x51FD;&#x6570;"></a>Function() &#x6784;&#x9020;&#x51FD;&#x6570;</h3><p>Function() &#x6784;&#x9020;&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x4F20;&#x5165;&#x4EFB;&#x610F;&#x6570;&#x91CF;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x5B9E;&#x53C2;&#xFF0C;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x5B9E;&#x53C2;&#x6240;&#x8868;&#x793A;&#x7684;&#x6587;&#x672C;&#x5C31;&#x662F;&#x51FD;&#x6570;&#x4F53;&#xFF0C;&#x5B83;&#x53EF;&#x4EE5;&#x5305;&#x542B;&#x4EFB;&#x610F;&#x7684; JavaScript &#x8BED;&#x53E5;&#xFF0C;&#x6BCF;&#x4E24;&#x6761;&#x8BED;&#x53E5;&#x4E4B;&#x95F4;&#x7528;&#x5206;&#x53F7;&#x5206;&#x9694;&#x3002;&#x4F20;&#x5165;&#x6784;&#x9020;&#x51FD;&#x6570;&#x7684;&#x5176;&#x5B83;&#x6240;&#x6709;&#x5B57;&#x7B26;&#x4E32;&#x662F;&#x6307;&#x5B9A;&#x51FD;&#x6570;&#x7684;&#x5F62;&#x53C2;&#x540D;&#x5B57;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x3002;&#x5982;&#x679C;&#x5B9A;&#x4E49;&#x7684;&#x51FD;&#x6570;&#x4E0D;&#x5305;&#x542B;&#x4EFB;&#x4F55;&#x53C2;&#x6570;&#xFF0C;&#x53EA;&#x9700;&#x8981;&#x7ED9;&#x6784;&#x9020;&#x51FD;&#x6570;&#x7B80;&#x5355;&#x7684;&#x4F20;&#x5165;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x5373;&#x51FD;&#x6570;&#x4F53;&#x3002;</p>
<p>&#x6CE8;&#xFF1A; Function() &#x6784;&#x9020;&#x51FD;&#x6570;&#x5E76;&#x4E0D;&#x9700;&#x8981;&#x901A;&#x8FC7;&#x4F20;&#x5165;&#x5B9E;&#x53C2;&#x4EE5;&#x6307;&#x5B9A;&#x51FD;&#x6570;&#x540D;&#x3002;&#x5C31;&#x50CF;&#x51FD;&#x6570;&#x7684;&#x76F4;&#x63A5;&#x91CF;&#x4E00;&#x6837;&#xFF0C;Function() &#x6784;&#x9020;&#x51FD;&#x6570;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x533F;&#x540D;&#x51FD;&#x6570;&#x3002;</p>
<ul>
<li>Function() &#x6784;&#x9020;&#x51FD;&#x6570;&#x5141;&#x8BB8; JavaScript &#x5728;&#x8FD0;&#x884C;&#x65F6;&#x52A8;&#x6001;&#x5730;&#x521B;&#x5EFA;&#x5E76;&#x7F16;&#x8BD1;&#x51FD;&#x6570;</li>
<li>&#x6BCF;&#x6B21;&#x8C03;&#x7528; Function() &#x6784;&#x9020;&#x51FD;&#x6570;&#x90FD;&#x4F1A;&#x89E3;&#x6790;&#x51FD;&#x6570;&#x4F53;&#xFF0C;&#x5E76;&#x521B;&#x5EFA;&#x51FD;&#x6570;&#x5BF9;&#x8C61;</li>
<li>Function() &#x6784;&#x9020;&#x51FD;&#x6570;&#x521B;&#x5EFA;&#x7684;&#x51FD;&#x6570;&#x5E76;&#x4E0D;&#x4F7F;&#x7528;&#x8BCD;&#x6CD5;&#x4F5C;&#x7528;&#x57DF;&#xFF0C;&#x51FD;&#x6570;&#x4F53;&#x4EE3;&#x7801;&#x7684;&#x7F16;&#x8BD1;&#x603B;&#x662F;&#x4F1A;&#x5728;&#x9876;&#x5C42;&#x51FD;&#x6570;&#x6267;&#x884C;&#x3002;</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> scope = <span class="string">&quot;global&quot;</span>;</div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">constructFunction</span>(<span class="params"></span>) </span>{</div><div class="line">    <span class="keyword">var</span> scope = <span class="string">&quot;local&quot;</span>;</div><div class="line">    <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Function</span>(<span class="string">&quot;return scope&quot;</span>);  <span class="comment">// &#x8FD9;&#x91CC;&#x65E0;&#x6CD5;&#x4F7F;&#x7528;&#x5C40;&#x90E8;&#x4F5C;&#x7528;&#x57DF;&#x7684;&#x53D8;&#x91CF;</span></div><div class="line">}</div><div class="line"><span class="comment">// &#x901A;&#x8FC7; Function() &#x6784;&#x9020;&#x51FD;&#x6570;&#x5B9A;&#x4E49;&#x7684;&#x51FD;&#x6570;&#x4F7F;&#x7528;&#x7684;&#x4E0D;&#x662F;&#x5C40;&#x90E8;&#x4F5C;&#x7528;&#x57DF;</span></div><div class="line"><span class="built_in">console</span>.log(constructFunction()());   <span class="comment">// &quot;golobal&quot;</span></div></pre></td></tr></table></figure>
<p>&#x53EF;&#x4EE5;&#x8BA4;&#x4E3A; Function() &#x6784;&#x9020;&#x51FD;&#x6570;&#x662F;&#x5728;&#x5168;&#x5C40;&#x4F5C;&#x7528;&#x57DF;&#x4E2D;&#x6267;&#x884C;&#x7684; eval(); eval() &#x53EF;&#x4EE5;&#x5728;&#x81EA;&#x5DF1;&#x7684;&#x79C1;&#x6709;&#x4F5C;&#x7528;&#x57DF;&#x5185;&#x5B9A;&#x4E49;&#x65B0;&#x53D8;&#x91CF;&#x548C;&#x51FD;&#x6570;&#x3002; Function() &#x5F88;&#x5C11;&#x7528;&#x5230;&#x3002;</p>
<h3 id="&#x53EF;&#x8C03;&#x7528;&#x7684;&#x5BF9;&#x8C61;"><a href="#&#x53EF;&#x8C03;&#x7528;&#x7684;&#x5BF9;&#x8C61;" class="headerlink" title="&#x53EF;&#x8C03;&#x7528;&#x7684;&#x5BF9;&#x8C61;"></a>&#x53EF;&#x8C03;&#x7528;&#x7684;&#x5BF9;&#x8C61;</h3><p>&#x7C7B;&#x4F3C;&#x4E8E; &#x6240;&#x6709;&#x7684;&#x201D;&#x4F2A;&#x6570;&#x7EC4;&#x201D;&#xFF0C;&#x5BF9;&#x4E8E;&#x51FD;&#x6570;&#x4E5F;&#x5B58;&#x5728;&#x7C7B;&#x4F3C;&#x7684;&#x60C5;&#x51B5;&#x3002;&#x201D;&#x53EF;&#x8C03;&#x7528;&#x5BF9;&#x8C61;&#x201D; &#x662F;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x8868;&#x8FBE;&#x5F0F;&#x4E2D;&#x8C03;&#x7528;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x3002;&#x6240;&#x6709;&#x7684;&#x51FD;&#x6570;&#x90FD;&#x662F;&#x53EF;&#x8C03;&#x7528;&#x7684;&#xFF0C;&#x4F46;&#x5E76;&#x975E;&#x6240;&#x6709;&#x7684;&#x53EF;&#x8C03;&#x7528;&#x5BF9;&#x8C61;&#x90FD;&#x662F;&#x51FD;&#x6570;&#x3002;</p>
<p>&#x53EF;&#x8C03;&#x7528;&#x5BF9;&#x8C61;&#x5728;&#x4E24;&#x4E2A; JavaScript &#x5B9E;&#x73B0;&#x4E2D;&#x4E0D;&#x80FD;&#x7B97;&#x4F5C;&#x51FD;&#x6570;&#x3002;&#x9996;&#x5148;&#xFF0C;IE Web &#x6D4F;&#x89C8;&#x5668;&#x5B9E;&#x73B0;&#x4E86;&#x5BA2;&#x6237;&#x7AEF;&#x65B9;&#x6CD5;&#xFF0C;&#x6BD4;&#x5982; Window.alert(), Document.getElementById(), &#x4F7F;&#x7528;&#x4E86;&#x53EF;&#x8C03;&#x7528;&#x7684;&#x5BBF;&#x4E3B;&#x5BF9;&#x8C61;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x5185;&#x7F6E;&#x51FD;&#x6570;&#x5BF9;&#x8C61;&#x3002;</p>
<p>&#x53E6;&#x4E00;&#x4E2A;&#x53EF;&#x8C03;&#x7528;&#x5BF9;&#x8C61;&#x662F; RegExp &#x5BF9;&#x8C61;&#xFF0C;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x8C03;&#x7528; RegExp &#x5BF9;&#x8C61;&#xFF0C;&#x8FD9;&#x6BD4;&#x8C03;&#x7528;&#x5B83;&#x7684; exec() &#x65B9;&#x6CD5;&#x66F4;&#x5FEB;&#x4E00;&#x4E9B;&#x3002;&#x8FD9;&#x662F; JavaScript &#x4E2D;&#x7684;&#x4E00;&#x4E2A;&#x975E;&#x6807;&#x51C6;&#x7279;&#x6027;&#xFF0C;&#x4F7F;&#x7528;typeof &#x8FD0;&#x7B97;&#x7684;&#x7ED3;&#x679C;&#x5E76;&#x4E0D;&#x7EDF;&#x4E00;&#x3002;</p>
<h2 id="&#x51FD;&#x6570;&#x5F0F;&#x7F16;&#x7A0B;"><a href="#&#x51FD;&#x6570;&#x5F0F;&#x7F16;&#x7A0B;" class="headerlink" title="&#x51FD;&#x6570;&#x5F0F;&#x7F16;&#x7A0B;"></a>&#x51FD;&#x6570;&#x5F0F;&#x7F16;&#x7A0B;</h2><h3 id="&#x4F7F;&#x7528;&#x51FD;&#x6570;&#x5904;&#x7406;&#x6570;&#x7EC4;"><a href="#&#x4F7F;&#x7528;&#x51FD;&#x6570;&#x5904;&#x7406;&#x6570;&#x7EC4;" class="headerlink" title="&#x4F7F;&#x7528;&#x51FD;&#x6570;&#x5904;&#x7406;&#x6570;&#x7EC4;"></a>&#x4F7F;&#x7528;&#x51FD;&#x6570;&#x5904;&#x7406;&#x6570;&#x7EC4;</h3><p>ECMAScript 3 &#x4E2D;&#x6CA1;&#x6709;&#x6570;&#x7EC4;&#x7684; map() &#x548C; reduce() &#x51FD;&#x6570;&#xFF0C;&#x5C01;&#x88C5;&#x517C;&#x5BB9;&#x7684; map() &#x548C; reduce()&#x3002;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// &#x5BF9;&#x4E8E;&#x6570;&#x7EC4;&#x7684;&#x6BCF;&#x4E2A;&#x5143;&#x7D20;&#x8C03;&#x7528;&#x51FD;&#x6570;&#xFF0C;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;</span></div><div class="line"><span class="keyword">var</span> map = <span class="built_in">Array</span>.prototype.map </div><div class="line">  ? <span class="function"><span class="keyword">function</span>(<span class="params">a, callback</span>) </span>{ <span class="keyword">return</span> a.map(callback); } </div><div class="line">  : <span class="function"><span class="keyword">function</span>(<span class="params">a, callback</span>) </span>{</div><div class="line">    <span class="keyword">var</span> results = [];</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>, len = a.length; i &lt; len; i++) {</div><div class="line">      <span class="keyword">if</span> (i <span class="keyword">in</span> a) results[i] = callback.call(<span class="literal">null</span>, a[i], i, a);</div><div class="line">    }</div><div class="line">    <span class="keyword">return</span> results;</div><div class="line">  };</div><div class="line"><span class="comment">// &#x5982;&#x679C;&#x51FD;&#x6570; callback &#x548C;&#x53EF;&#x9009;&#x7684;&#x521D;&#x59CB;&#x503C;&#x5C06;&#x6570;&#x7EC4; a &#x51CF;&#x81F3;&#x4E00;&#x4E2A;&#x503C;</span></div><div class="line"><span class="keyword">var</span> reduce = <span class="built_in">Array</span>.prototype.reduce</div><div class="line">  ? <span class="function"><span class="keyword">function</span>(<span class="params">a, callback, initial</span>) </span>{</div><div class="line">    <span class="keyword">if</span> (<span class="built_in">arguments</span>.length &gt; <span class="number">2</span>) </div><div class="line">      <span class="keyword">return</span> a.reduce(callback, initial);   <span class="comment">// &#x5982;&#x679C;&#x4F20;&#x5165;&#x4E86;&#x4E00;&#x4E2A;&#x521D;&#x59CB;&#x503C;</span></div><div class="line">    <span class="keyword">else</span> <span class="keyword">return</span> a.reduce(callback); <span class="comment">// &#x5426;&#x5219;&#x6CA1;&#x6709;&#x521D;&#x59CB;&#x503C;</span></div><div class="line">  }</div><div class="line">  : funciton(a, callback, initial) {</div><div class="line">    <span class="keyword">var</span> i = <span class="number">0</span>, len = a.length, accumulator;</div><div class="line">    <span class="keyword">if</span> (<span class="built_in">arguments</span>.length &gt; <span class="number">2</span>) accumulator = initial;</div><div class="line">    <span class="keyword">else</span> { <span class="comment">// &#x627E;&#x5230;&#x6570;&#x7EC4;&#x4E2D;&#x5DF2;&#x5B9A;&#x4E49;&#x7684;&#x7D22;&#x5F15;</span></div><div class="line">      <span class="keyword">if</span> (len == <span class="number">0</span>) <span class="keyword">throw</span> <span class="built_in">TypeError</span>();</div><div class="line">      <span class="keyword">while</span>(i &lt; len) {</div><div class="line">        <span class="keyword">if</span> (i <span class="keyword">in</span> a) {</div><div class="line">          accumulator = a[i++];</div><div class="line">          <span class="keyword">break</span>;</div><div class="line">        }</div><div class="line">        <span class="keyword">else</span> i++;</div><div class="line">      }</div><div class="line">      <span class="keyword">if</span> (i == len) <span class="keyword">throw</span> <span class="built_in">TypeError</span>();</div><div class="line">    }</div><div class="line">    <span class="comment">// &#x5BF9;&#x6570;&#x7EC4;&#x5269;&#x4F59;&#x7684;&#x5143;&#x7D20;&#x4F9D;&#x6B21;&#x8C03;&#x7528; callback</span></div><div class="line">    <span class="keyword">while</span> (i &lt; len) {</div><div class="line">      <span class="keyword">if</span> (i <span class="keyword">in</span> a)</div><div class="line">        accumulator = callback.call(<span class="literal">undefined</span>, accumulator, a[i], i, a);</div><div class="line">      i++;</div><div class="line">    }</div><div class="line">    <span class="keyword">return</span> accumulator;</div><div class="line">  };</div></pre></td></tr></table></figure>
<h2 id="&#x9012;&#x5F52;&#xFF1A;&#x51FD;&#x6570;&#x81EA;&#x5DF1;&#x8C03;&#x7528;&#x81EA;&#x5DF1;"><a href="#&#x9012;&#x5F52;&#xFF1A;&#x51FD;&#x6570;&#x81EA;&#x5DF1;&#x8C03;&#x7528;&#x81EA;&#x5DF1;" class="headerlink" title="&#x9012;&#x5F52;&#xFF1A;&#x51FD;&#x6570;&#x81EA;&#x5DF1;&#x8C03;&#x7528;&#x81EA;&#x5DF1;"></a>&#x9012;&#x5F52;&#xFF1A;&#x51FD;&#x6570;&#x81EA;&#x5DF1;&#x8C03;&#x7528;&#x81EA;&#x5DF1;</h2><h3 id="&#x8BA1;&#x7B97;&#x6590;&#x6CE2;&#x90A3;&#x5951;&#x6570;&#x5217;&#x7B2C;n&#x9879;&#x7684;&#x503C;&#xFF1A;1-1-2-3-5-8-13&#x2026;"><a href="#&#x8BA1;&#x7B97;&#x6590;&#x6CE2;&#x90A3;&#x5951;&#x6570;&#x5217;&#x7B2C;n&#x9879;&#x7684;&#x503C;&#xFF1A;1-1-2-3-5-8-13&#x2026;" class="headerlink" title="&#x8BA1;&#x7B97;&#x6590;&#x6CE2;&#x90A3;&#x5951;&#x6570;&#x5217;&#x7B2C;n&#x9879;&#x7684;&#x503C;&#xFF1A;1,1,2,3,5,8,13&#x2026;"></a>&#x8BA1;&#x7B97;&#x6590;&#x6CE2;&#x90A3;&#x5951;&#x6570;&#x5217;&#x7B2C;n&#x9879;&#x7684;&#x503C;&#xFF1A;1,1,2,3,5,8,13&#x2026;</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">fibonacci</span>(<span class="params">n</span>)</span>{</div><div class="line">    <span class="keyword">if</span>(n==<span class="number">1</span> || n==<span class="number">2</span>) <span class="keyword">return</span> <span class="number">1</span>;</div><div class="line">    <span class="keyword">return</span> fibonacci(n<span class="number">-1</span>)+fibonacci(n<span class="number">-2</span>);</div><div class="line">}</div><div class="line"><span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="number">10</span>; i++) { <span class="built_in">console</span>.log(fibonacci(i+<span class="number">1</span>)); }</div></pre></td></tr></table></figure>
<h3 id="&#x9012;&#x5F52;&#x8BA1;&#x7B97;&#x9636;&#x4E58;"><a href="#&#x9012;&#x5F52;&#x8BA1;&#x7B97;&#x9636;&#x4E58;" class="headerlink" title="&#x9012;&#x5F52;&#x8BA1;&#x7B97;&#x9636;&#x4E58;"></a>&#x9012;&#x5F52;&#x8BA1;&#x7B97;&#x9636;&#x4E58;</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">factorial</span>(<span class="params">n</span>)</span>{</div><div class="line">    <span class="keyword">if</span>(n&lt;<span class="number">0</span>) <span class="keyword">return</span> <span class="number">0</span>; <span class="comment">//&#x4E3A;&#x4E86;&#x9632;&#x6B62;&#x62A5;&#x9519;</span></div><div class="line">    <span class="keyword">if</span>(n==<span class="number">0</span>) <span class="keyword">return</span> <span class="number">1</span>; <span class="comment">//&#x9012;&#x5F52;&#x7684;&#x7ED3;&#x675F;&#x6761;&#x4EF6;&#xFF1A;0&#x7684;&#x9636;&#x4E58;&#x4E3A;1</span></div><div class="line">    <span class="keyword">return</span> factorial(n<span class="number">-1</span>)*n;</div><div class="line">}</div><div class="line"><span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="number">10</span>; i++) { <span class="built_in">console</span>.log(<span class="string">&quot;&#x6570;&#x5B57;&#xFF1A;&quot;</span>+i); <span class="built_in">console</span>.log(factorial(i));}</div></pre></td></tr></table></figure>
<h3 id="m&#x7684;n&#x6B21;&#x65B9;"><a href="#m&#x7684;n&#x6B21;&#x65B9;" class="headerlink" title="m&#x7684;n&#x6B21;&#x65B9;"></a>m&#x7684;n&#x6B21;&#x65B9;</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">pow</span>(<span class="params">n, m</span>) </span>{</div><div class="line">      <span class="keyword">if</span> (m === <span class="number">0</span>) <span class="keyword">return</span> <span class="number">1</span>;</div><div class="line">      <span class="keyword">if</span> (m &lt; <span class="number">0</span>) <span class="keyword">return</span> <span class="number">1</span> / (pow(n, -(m + <span class="number">1</span>)) * n);</div><div class="line">      <span class="keyword">else</span> <span class="keyword">if</span> (m &gt; <span class="number">0</span>) <span class="keyword">return</span> pow(n, m - <span class="number">1</span>) * n;</div><div class="line">  }</div><div class="line">  <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">-2</span>; i &lt;= <span class="number">0</span>; i++) { <span class="built_in">console</span>.log(pow(<span class="number">2</span>, i)); }</div></pre></td></tr></table></figure>
<h3 id="&#x9012;&#x5F52;&#x67E5;&#x627E;&#x7236;&#x5143;&#x7D20;"><a href="#&#x9012;&#x5F52;&#x67E5;&#x627E;&#x7236;&#x5143;&#x7D20;" class="headerlink" title="&#x9012;&#x5F52;&#x67E5;&#x627E;&#x7236;&#x5143;&#x7D20;"></a>&#x9012;&#x5F52;&#x67E5;&#x627E;&#x7236;&#x5143;&#x7D20;</h3><p><strong>&#x9700;&#x6C42;&#xFF1A;&#x8981;&#x5224;&#x65AD;&#x4E00;&#x4E2A;div&#x662F;&#x5426;&#x5728;&#x53E6;&#x4E00;&#x4E2A;div&#x7684;&#x4E0B;&#x9762;</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">find</span>(<span class="params">child,parent</span>)</span>{</div><div class="line">    <span class="comment">//&#x5B9E;&#x73B0;&#x601D;&#x8DEF;&#xFF1A;&#x7531;&#x5B50;&#x5143;&#x7D20;&#x4E00;&#x7EA7;&#x4E00;&#x7EA7;&#x7684;&#x67E5;&#x627E;&#x7236;&#x5143;&#x7D20;</span></div><div class="line">    <span class="comment">//&#x9012;&#x5F52;&#x7684;&#x7ED3;&#x675F;&#x6761;&#x4EF6;&#xFF1A;&#x67E5;&#x5230;&#x4E86;&#x6587;&#x6863;&#x7684;&#x6839;&#x8282;&#x70B9;&#x3001;&#x627E;&#x5230;&#x4E86;&#x7236;&#x5143;&#x7D20;</span></div><div class="line">    <span class="keyword">if</span>(child.parentNode===parent) <span class="keyword">return</span> <span class="literal">true</span>; <span class="comment">//&#x8BF4;&#x660E;&#x5DF2;&#x7ECF;&#x627E;&#x5230;&#x4E86;&#x7B26;&#x5408;&#x6761;&#x4EF6;&#x7684;&#x7236;&#x5143;&#x7D20;</span></div><div class="line">    <span class="keyword">if</span>(child.parentNode===<span class="literal">null</span>) <span class="keyword">return</span> <span class="literal">false</span>; <span class="comment">//&#x8BF4;&#x660E;&#x5DF2;&#x7ECF;&#x67E5;&#x627E;&#x5230;&#x4E86;&#x6587;&#x6863;&#x7684;&#x6839;&#x8282;&#x70B9;</span></div><div class="line">    <span class="keyword">return</span> find(child.parentNode,parent);</div><div class="line">    <span class="comment">//&#x7B2C;1&#x6B21;&#x6267;&#x884C;find&#x2014;&#x2014;&gt;child.parentNode===parent</span></div><div class="line">    <span class="comment">//&#x7B2C;2&#x6B21;&#x6267;&#x884C;find&#x2014;&#x2014;&gt;child.parentNode.parentNode===parent</span></div><div class="line">    <span class="comment">//&#x7B2C;3&#x6B21;&#x6267;&#x884C;find&#x2014;&#x2014;&gt;child.parentNode.parentNode.parentNode===parent</span></div><div class="line">}</div><div class="line"><span class="built_in">console</span>.log(find(d3,d10));<span class="comment">//false</span></div><div class="line"><span class="built_in">console</span>.log(find(d3,d1));<span class="comment">//true</span></div></pre></td></tr></table></figure>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        
  <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
    <div>感谢您的支持！</div>
    <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
      <span>打 赏</span>
    </button>
    <div id="QR" style="display: none;">
      
        <div id="wechat" style="display: inline-block">
          <img id="wechat_qr" src="http://ofy7k3v4s.bkt.clouddn.com/wcpayCode.jpg" alt="Hongqin Ma WeChat Pay"/>
          <p>微信打赏</p>
        </div>
      
      
        <div id="alipay" style="display: inline-block">
          <img id="alipay_qr" src="http://ofy7k3v4s.bkt.clouddn.com/apayCode.jpg" alt="Hongqin Ma Alipay"/>
          <p>支付宝打赏</p>
        </div>
      
    </div>
  </div>


      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/js/" rel="tag">#js</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2014/05/15/09-js-String对象/" rel="next" title="js String对象">
                <i class="fa fa-chevron-left"></i> js String对象
              </a>
            
          </div>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2014/06/03/11-面向对象的程序设计/" rel="prev" title="面向对象的程序设计">
                面向对象的程序设计 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
        <div class="ds-share flat" data-thread-key="2014/05/24/10-js-的函数/"
     data-title="js 的函数"
     data-content=""
     data-url="https://hongqinma.github.io/2014/05/24/10-js-的函数/">
  <div class="ds-share-inline">
    <ul  class="ds-share-icons-16">

      <li data-toggle="ds-share-icons-more"><a class="ds-more" href="javascript:void(0);">分享到：</a></li>
      <li><a class="ds-weibo" href="javascript:void(0);" data-service="weibo">微博</a></li>
      <li><a class="ds-qzone" href="javascript:void(0);" data-service="qzone">QQ空间</a></li>
      <li><a class="ds-qqt" href="javascript:void(0);" data-service="qqt">腾讯微博</a></li>
      <li><a class="ds-wechat" href="javascript:void(0);" data-service="wechat">微信</a></li>

    </ul>
    <div class="ds-share-icons-more">
    </div>
  </div>
</div>
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
      <div class="ds-thread" data-thread-key="2014/05/24/10-js-的函数/"
           data-title="js 的函数" data-url="https://hongqinma.github.io/2014/05/24/10-js-的函数/">
      </div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            Overview
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel ">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="//schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/photo.jpg"
               alt="Hongqin Ma" />
          <p class="site-author-name" itemprop="name">Hongqin Ma</p>
          <p class="site-description motion-element" itemprop="description">Spell at all costs， to rush you in the future. </p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">68</span>
              <span class="site-state-item-name">posts</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              <a href="/categories">
                <span class="site-state-item-count">16</span>
                <span class="site-state-item-name">categories</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">57</span>
                <span class="site-state-item-name">tags</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/HongqinMa" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://twitter.com/HongqinMa" target="_blank" title="Twitter">
                  
                    <i class="fa fa-fw fa-twitter"></i>
                  
                  Twitter
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://www.facebook.com/profile.php?id=100006581058735" target="_blank" title="Facebook">
                  
                    <i class="fa fa-fw fa-facebook"></i>
                  
                  Facebook
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://www.zhihu.com/people/HirakuMa" target="_blank" title="知乎">
                  
                    <i class="fa fa-fw fa-share-alt"></i>
                  
                  知乎
                </a>
              </span>
            
          
        </div>

        
        

        
        
          <div class="links-of-blogroll motion-element links-of-blogroll-inline">
            <div class="links-of-blogroll-title">
              <i class="fa  fa-fw fa-globe"></i>
              Links
            </div>
            <ul class="links-of-blogroll-list">
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.cnblogs.com/rubylouvre/" title="司徒正美" target="_blank">司徒正美</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.cnblogs.com/sanshi/" title="三生石上" target="_blank">三生石上</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.cnblogs.com/TomXu/" title="汤姆大叔" target="_blank">汤姆大叔</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://gold.xitu.io/" title="掘金" target="_blank">掘金</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="https://segmentfault.com/" title="segmentfault" target="_blank">segmentfault</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.jianshu.com/users/1cbb94e5ed4d/latest_articles" title="简书" target="_blank">简书</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="https://home.cnblogs.com/" title="博客园" target="_blank">博客园</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="https://www.awesomes.cn/" title="Awesomes" target="_blank">Awesomes</a>
                </li>
              
            </ul>
          </div>
        

      </section>

      
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">
            
              
            
            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#函数的定义"><span class="nav-number">1.</span> <span class="nav-text">函数的定义</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#函数定义方式一：-函数声明"><span class="nav-number">1.1.</span> <span class="nav-text">函数定义方式一： 函数声明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#函数的定义方式二：-函数表达式"><span class="nav-number">1.2.</span> <span class="nav-text">函数的定义方式二： 函数表达式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#函数的嵌套"><span class="nav-number">1.3.</span> <span class="nav-text">函数的嵌套</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#函数调用、this指向、返回值"><span class="nav-number">2.</span> <span class="nav-text">函数调用、this指向、返回值</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#函数的4种调用模式"><span class="nav-number">2.1.</span> <span class="nav-text">函数的4种调用模式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#不同调用模式中的this的值"><span class="nav-number">2.2.</span> <span class="nav-text">不同调用模式中的this的值</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#调用模式中的返回值"><span class="nav-number">2.3.</span> <span class="nav-text">调用模式中的返回值</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#函数的实参和形参"><span class="nav-number">3.</span> <span class="nav-text">函数的实参和形参</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#函数的可选形参"><span class="nav-number">3.1.</span> <span class="nav-text">函数的可选形参</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#可变长的实参列表、实参对象"><span class="nav-number">3.2.</span> <span class="nav-text">可变长的实参列表、实参对象</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#callee-和-caller-属性"><span class="nav-number">3.3.</span> <span class="nav-text">callee 和 caller 属性</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#将对象的属性作为实参"><span class="nav-number">3.4.</span> <span class="nav-text">将对象的属性作为实参</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#实参类型"><span class="nav-number">3.5.</span> <span class="nav-text">实参类型</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#作为值的函数"><span class="nav-number">4.</span> <span class="nav-text">作为值的函数</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#自定义函数的属性"><span class="nav-number">4.1.</span> <span class="nav-text">自定义函数的属性</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#作为命名空间的函数"><span class="nav-number">5.</span> <span class="nav-text">作为命名空间的函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#js-词法分析"><span class="nav-number">6.</span> <span class="nav-text">js 词法分析</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#程序执行过程"><span class="nav-number">6.1.</span> <span class="nav-text">程序执行过程</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#词法作用域（作用域：变量可以使用到不能使用的范围）"><span class="nav-number">6.2.</span> <span class="nav-text">词法作用域（作用域：变量可以使用到不能使用的范围）</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#严格模式"><span class="nav-number">7.</span> <span class="nav-text">严格模式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#闭包"><span class="nav-number">8.</span> <span class="nav-text">闭包</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#闭包实现思路：外层函数，内层函数"><span class="nav-number">8.1.</span> <span class="nav-text">闭包实现思路：外层函数，内层函数</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#函数的属性、方法和构造函数"><span class="nav-number">9.</span> <span class="nav-text">函数的属性、方法和构造函数</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#length-属性"><span class="nav-number">9.1.</span> <span class="nav-text">length 属性</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#prototype-属性"><span class="nav-number">9.2.</span> <span class="nav-text">prototype 属性</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#call-方法和-apply-方法"><span class="nav-number">9.3.</span> <span class="nav-text">call() 方法和 apply() 方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#call-apply区别"><span class="nav-number">9.4.</span> <span class="nav-text">call/apply区别</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#apply的一个漂亮的应用"><span class="nav-number">9.5.</span> <span class="nav-text">apply的一个漂亮的应用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#将当前函数的-arguments-数组直接传入-apply-来调用另一个函数"><span class="nav-number">9.6.</span> <span class="nav-text">将当前函数的 arguments 数组直接传入 apply() 来调用另一个函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#bind-方法"><span class="nav-number">9.7.</span> <span class="nav-text">bind() 方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#bind-的兼容方法"><span class="nav-number">9.8.</span> <span class="nav-text">bind() 的兼容方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#toString-方法"><span class="nav-number">9.9.</span> <span class="nav-text">toString() 方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#静态属性和实例属性"><span class="nav-number">9.10.</span> <span class="nav-text">静态属性和实例属性</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#所有的函数对象都共有的一些静态属性"><span class="nav-number">9.11.</span> <span class="nav-text">所有的函数对象都共有的一些静态属性</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Function-构造函数"><span class="nav-number">9.12.</span> <span class="nav-text">Function() 构造函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#可调用的对象"><span class="nav-number">9.13.</span> <span class="nav-text">可调用的对象</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#函数式编程"><span class="nav-number">10.</span> <span class="nav-text">函数式编程</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#使用函数处理数组"><span class="nav-number">10.1.</span> <span class="nav-text">使用函数处理数组</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#递归：函数自己调用自己"><span class="nav-number">11.</span> <span class="nav-text">递归：函数自己调用自己</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#计算斐波那契数列第n项的值：1-1-2-3-5-8-13…"><span class="nav-number">11.1.</span> <span class="nav-text">计算斐波那契数列第n项的值：1,1,2,3,5,8,13…</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#递归计算阶乘"><span class="nav-number">11.2.</span> <span class="nav-text">递归计算阶乘</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#m的n次方"><span class="nav-number">11.3.</span> <span class="nav-text">m的n次方</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#递归查找父元素"><span class="nav-number">11.4.</span> <span class="nav-text">递归查找父元素</span></a></li></ol></li></ol></div>
            
          </div>
        </section>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2016</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Hongqin Ma</span>
</div>

<div class="powered-by">
  Powered by <a class="theme-link" href="https://hexo.io">Hexo</a>
</div>

<div class="theme-info">
  Theme -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>

<script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<span id="busuanzi_container_site_pv">
    &nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;Page Views: <span id="busuanzi_value_site_pv"></span> 
</span>

        

<div class="busuanzi-count">

  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv"><i class="fa fa-user"></i><span class="busuanzi-value" id="busuanzi_value_site_uv"></span></span>
  

  
    <span class="site-pv"><i class="fa fa-eye"></i><span class="busuanzi-value" id="busuanzi_value_site_pv"></span></span>
  
  
</div>



        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.0.2"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.0.2"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.2"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.2"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.2"></script>



  

  
    
  

  <script type="text/javascript">
    var duoshuoQuery = {short_name:"Hiraku"};
    (function() {
      var ds = document.createElement('script');
      ds.type = 'text/javascript';ds.async = true;
      ds.id = 'duoshuo-script';
      ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
      ds.charset = 'UTF-8';
      (document.getElementsByTagName('head')[0]
      || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
  </script>

  
    
    <script src="/lib/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>
    <script src="/js/src/hook-duoshuo.js"></script>
  






  
  

  

  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.1.js"></script>
  <script>AV.initialize("PPmoIQxeu8Sd21h6Tl9TthNx-gzGzoHsz", "rTvbY2H7DvWzRipiBwn72RQ4");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  

  
  
  <!-- custom analytics part create by xiamo -->
<script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.1.js"></script>
<script>AV.initialize("PPmoIQxeu8Sd21h6Tl9TthNx-gzGzoHsz", "rTvbY2H7DvWzRipiBwn72RQ4");</script>
<script>
function showTime(Counter) {
  var query = new AV.Query(Counter);
  $(".leancloud_visitors").each(function() {
    var url = $(this).attr("id").trim();
    query.equalTo("url", url);
    query.find({
      success: function(results) {
        if (results.length == 0) {
          var content = '0 ' + $(document.getElementById(url)).text();
          $(document.getElementById(url)).text(content);
          return;
        }
        for (var i = 0; i < results.length; i++) {
          var object = results[i];
          var content = object.get('time') + ' ' + $(document.getElementById(url)).text();
          $(document.getElementById(url)).text(content);
        }
      },
      error: function(object, error) {
        console.log("Error: " + error.code + " " + error.message);
      }
    });

  });
}

function addCount(Counter) {
  var Counter = AV.Object.extend("Counter");
  url = $(".leancloud_visitors").attr('id').trim();
  title = $(".leancloud_visitors").attr('data-flag-title').trim();
  var query = new AV.Query(Counter);
  query.equalTo("url", url);
  query.find({
    success: function(results) {
      if (results.length > 0) {
        var counter = results[0];
        counter.fetchWhenSave(true);
        counter.increment("time");
        counter.save(null, {
          success: function(counter) {
            var content =  counter.get('time') + ' ' + $(document.getElementById(url)).text();
            $(document.getElementById(url)).text(content);
          },
          error: function(counter, error) {
            console.log('Failed to save Visitor num, with error message: ' + error.message);
          }
        });
      } else {
        var newcounter = new Counter();
        newcounter.set("title", title);
        newcounter.set("url", url);
        newcounter.set("time", 1);
        newcounter.save(null, {
          success: function(newcounter) {
              console.log("newcounter.get('time')="+newcounter.get('time'));
            var content = newcounter.get('time') + ' ' + $(document.getElementById(url)).text();
            $(document.getElementById(url)).text(content);
          },
          error: function(newcounter, error) {
            console.log('Failed to create');
          }
        });
      }
    },
    error: function(error) {
      console.log('Error:' + error.code + " " + error.message);
    }
  });
}
$(function() {
  var Counter = AV.Object.extend("Counter");
  if ($('.leancloud_visitors').length == 1) {
    addCount(Counter);
  } else if ($('.post-title-link').length > 1) {
    showTime(Counter);
  }
}); 
</script>

  
</body>
</html>
